Jump to content

[QUESTION] SQL Database problem


DriFtyZ

Recommended Posts

Hello fellas i have an important question about the databases in lua scripting, i have made a carshop system which stores the vehicles inside the database and i have made a menu which the user selects to spawn despawn his vehicle. if the user has 2 same vehicles how the script will know wich one from the database will pick?
image of database:

PgwN4iM.png

Edited by Dutchman101
Fix topic title for section requirements
Link to comment

Basically every single time you create a new SQL table, you will need a primary key with auto increment. This is a number that gets incremented every time you insert a new row. You don't have to do anything with it, SQL will handle that for you, all you have to do is create the column. 

This way every vehicle will have a unique ID.

Link to comment

i want to make vehicle ids but for the each player specific i mean if the user driftyz has 2 vehicle there should his name in the first column on the first 2 rows and then id 1 which means his first vehicle on the list and id 2 but if some other user buy  a car i don't want him ta have the same id increasement as driftyz but 1 again as its gonna be his first car

Link to comment

If this is SQLite, I believe all tables have by default a column "rowid" which is kinda hidden by default, and not selected when selecting *, so just try to SELECT rowid, * FROM that table and then use the rowid column for identification - for example, in a GUI with a gridlist, you'd want to guiGridListGetItemData to store the rowid there, and send the server a spawn request along with that rowid so the server can just select WHERE rowid = ?.

6CwC2Ih.jpgThis is the rowid column ^

Edited by MrTasty
Link to comment

to be more specific when the user clicks the spawn button on gui it triggers the server event which is the follow:

addEvent("spawnVehicleS", true)
function spawnVehicleS ( player, pX, pY, pZ, vehNameS, vehPlateS )
	if player and pX and pY and pZ and vehNameS and vehPlateS then
		if playerVeh1 then
			destroyElement(playerVeh1)
		end
		local accName = getAccountName(getPlayerAccount(player))
		local vehicleQuery = dbQuery(vehicleDb,"SELECT * FROM vehicles WHERE ownerName=? AND veh_name=? AND veh_plate=?", accName, vehNameS, vehPlateS)
		local vehicleQueryResult = dbPoll(vehicleQuery, -1)
		if #vehicleQueryResult > 0 then
			for rid, row in ipairs (vehicleQueryResult) do
				local veh_id = row.veh_id
				local veh_plate = row.veh_plate
				local colR1 = row.colR1
				local colR2 = row.colR2
				local colR3 = row.colR3
				local colG1 = row.colG1
				local colG2 = row.colG2
				local colG3 = row.colG3
				local colB1 = row.colB1
				local colB2 = row.colB2
				local colB3 = row.colB3
				local col_hs1 = row.col_hs1
				local col_hs2 = row.col_hs2
				local col_hs3 = row.col_hs3
				local veh_Health = row.veh_Health
				playerVeh1 = createVehicle(veh_id, pX, pY, pZ, 0, 0, 0, veh_plate)
				local setVehicle1Color = setVehicleColor(playerVeh1,colR1,colG1,colB1,colR2,colG2,colB2,colR3,colG3,colB3, 0, 0, 0)
				local setVehicle1LightsColor = setVehicleHeadLightColor(playerVeh1, veh_hs1, veh_hs2, veh_hs3)
				local playerToVeh = warpPedIntoVehicle(player, playerVeh1)
			end
		end
	end
end
addEventHandler("spawnVehicleS", getRootElement(), spawnVehicleS)

i know my problem is on the variable playerVeh1 but i don't know how to make a "unique" variable

Link to comment
-- at the top of the script, outside any function
local playerVehicles = {}

-- to destroy previous vehicle of this player (before spawning new vehicle, or when player disconnects, etc)
if isElement(playerVehicles[player]) then destroyElement(playerVehicles[player]) end

-- when spawning, store the vehicle as (don't forget to attempt to destroy BEFORE spawning a new one)
playerVehicles[player] = createVehicle(...)

 

Edited by MrTasty
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...