Jump to content

max car counter problem


Recommended Posts

Hi, i have a problem with my car counter, it should count every car i spawn from my gui selector but the problem is that it sets the count for everyone and not for the local player :oops:

here is my code so far:

local carCount = 0
maxCarCount = 1
car = {}
 
function createMyVehicle(vehicleid,x,y,z)
if vehicleid and x and y and z then
if carCount <= maxCarCount then
     carCount = carCount + 1
     car[carCount] = {}
     car[carCount].car = createVehicle(vehicleid,x,y,z)
setElementData ( car[carCount].car, "owner", source )
else
destroyElement(car[carCount].car)
  carCount = carCount - 1
end
end
end
addEvent("createVehicleFromGUI",true)
addEventHandler("createVehicleFromGUI",root,createMyVehicle)
 
function destroyVeh()
carCount = carCount - 1
end
addEventHandler("onVehicleExplode",getRootElement(),destroyVeh)

Link to comment

The problem is that you have one array, called car. Each time someone creates a car, the same array gets modified. What you should do, is have the array like

car = {}
-- in function
car[source] = {}
car[source][carCount].car = createVehicle()

So each player would have a separate table for their cars. Don't forget to clean the car table after player leaves though.

Link to comment
The problem is that you have one array, called car. Each time someone creates a car, the same array gets modified. What you should do, is have the array like
car = {}
-- in function
car[source] = {}
car[source][carCount].car = createVehicle()

So each player would have a separate table for their cars. Don't forget to clean the car table after player leaves though.

dosnt work it says at line of createVehicle "attempt to index field "?" (a nil value)"

this current code:

local carCount = 0
maxCarCount = 1
car = {}
 
function createMyVehicle(vehicleid,x,y,z)
if vehicleid and x and y and z then
if carCount <= maxCarCount then
     carCount = carCount + 1
     car[source] = {}
     car[source][carCount].car = createVehicle(vehicleid,x,y,z)
setElementData ( car[carCount].car, "owner", source )
else
destroyElement(car[carCount].car)
  carCount = carCount - 1
end
end
end
addEvent("createVehicleFromGUI",true)
addEventHandler("createVehicleFromGUI",root,createMyVehicle)
 
function destroyVeh()
carCount = carCount - 1
end
addEventHandler("onVehicleExplode",getRootElement(),destroyVeh)

Link to comment

oh well yes, you need to initiate car[source][carCount] as a table aswell. The error message is very descriptive, it's not just some blah blah.

Attempt to index a field "?" (a nil value)

You should understand from that that it attempts to do something like

randomtable[nil] = blah

car[source][carCount].car = createVehicle()

There is the [carCount].car the nil value, as car[source][carCount] isn't a table, because you haven't initiated it as a table.. A bit my fault too though, forgot to add car[source][carCount] = {} in my example.

@varez That's just not needed, if he defines it local there, it's local to the file scope, shouldn't have anything to do with the attempt to index a field "?" thingie.

Link to comment

Does this happen on the createVehicle or on the setElementData line?

You obviously have to change the tables there aswell, as there is no table called car[carCount] anymore. You have to change them everywhere to car[source][carCount]

Link to comment
carCount = 0
maxCarCount = 1
car = {}
 
function createMyVehicle(vehicleid,x,y,z)
if vehicleid and x and y and z then
if carCount <= maxCarCount then
     carCount = carCount + 1
     car[source][carCount] = {}
     car[source][carCount].car = createVehicle(vehicleid,x,y,z)
setElementData ( car[source][carCount].car, "owner", source )
else
destroyElement(car[source][carCount].car)
  carCount = carCount - 1
end
end
end
addEvent("createVehicleFromGUI",true)
addEventHandler("createVehicleFromGUI",root,createMyVehicle)
 
function destroyVeh()
carCount = carCount - 1
end
addEventHandler("onVehicleExplode",getRootElement(),destroyVeh)

makes a error in line "9" attempt to index field (a nil value)

Link to comment

Is the concept really that hard to grasp?

Now you don't declare the carCount[source] table anywhere.

If you have something like

tablea[b][c][d] = randomstuff

then you need to make sure that

- tablea is a table

- tablea is a table

- tablea[c] is a table

That explains better?

Link to comment

I tryed this before and does same problem.

this is code now.

carCount = 0
maxCarCount = 1
car = {}
 
function createMyVehicle(vehicleid,x,y,z)
if vehicleid and x and y and z then
if carCount <= maxCarCount then
     carCount = carCount + 1
     car[source] = {}
     car[source][carCount].car = createVehicle(vehicleid,x,y,z)
setElementData ( car[source][carCount].car, "owner", source )
else
destroyElement(car[source][carCount].car)
  carCount = carCount - 1
end
end
end
addEvent("createVehicleFromGUI",true)
addEventHandler("createVehicleFromGUI",root,createMyVehicle)
 
function destroyVeh()
carCount = carCount - 1
end
addEventHandler("onVehicleExplode",getRootElement(),destroyVeh)

Link to comment

like this should be?

carCount = 0
maxCarCount = 1
car = {}
 
function createMyVehicle(vehicleid,x,y,z)
if vehicleid and x and y and z then
if carCount <= maxCarCount then
     carCount = carCount + 1
     car[source] = {}
  car[source][carcount] = {}
     car[source][carCount].car = createVehicle(vehicleid,x,y,z)
setElementData ( car[source][carCount].car, "owner", source )
else
destroyElement(car[source][carCount].car)
  carCount = carCount - 1
end
end
end
addEvent("createVehicleFromGUI",true)
addEventHandler("createVehicleFromGUI",root,createMyVehicle)
 
function destroyVeh()
carCount = carCount - 1
end
addEventHandler("onVehicleExplode",getRootElement(),destroyVeh)

if yes then it still not working but now gives a different error,

server.lua:10: table index is nil

Link to comment

Indeed, lua is a case sensitive language. Look closely at line 10 and for example the previous line. I know I wrote carcount, but I'm sure if you actually LOOKED, you'd find the error at line 10. I'm not here to fix uppercase/lowercase stuff for your script, you have to do something yourself aswell.!

Link to comment
Indeed, lua is a case sensitive language. Look closely at line 10 and for example the previous line. I know I wrote carcount, but I'm sure if you actually LOOKED, you'd find the error at line 10. I'm not here to fix uppercase/lowercase stuff for your script, you have to do something yourself aswell.!

ahhh found it, thanks for all your help, you really helped me :D

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...