Castillo Posted July 4, 2010 Share Posted July 4, 2010 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 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
Lordy Posted July 4, 2010 Share Posted July 4, 2010 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
Castillo Posted July 4, 2010 Author Share Posted July 4, 2010 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
dzek (varez) Posted July 4, 2010 Share Posted July 4, 2010 local carCount = 0 remove local Link to comment
Lordy Posted July 4, 2010 Share Posted July 4, 2010 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
Castillo Posted July 4, 2010 Author Share Posted July 4, 2010 still same error but now dosnt say "a nil value" Link to comment
Lordy Posted July 4, 2010 Share Posted July 4, 2010 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
Castillo Posted July 4, 2010 Author Share Posted July 4, 2010 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
Lordy Posted July 4, 2010 Share Posted July 4, 2010 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
Castillo Posted July 4, 2010 Author Share Posted July 4, 2010 I dont understand what exactly u mean Link to comment
Lordy Posted July 4, 2010 Share Posted July 4, 2010 Before car[source][carCount] = {} you need to have car[source] = {} Link to comment
Castillo Posted July 4, 2010 Author Share Posted July 4, 2010 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
Castillo Posted July 5, 2010 Author Share Posted July 5, 2010 Anyone know how to make it working ? Link to comment
Lordy Posted July 5, 2010 Share Posted July 5, 2010 Now you removed car[source][carcount] = {} You need to have BOTH Link to comment
Castillo Posted July 5, 2010 Author Share Posted July 5, 2010 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
Lordy Posted July 5, 2010 Share Posted July 5, 2010 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
Castillo Posted July 5, 2010 Author Share Posted July 5, 2010 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 Link to comment
Castillo Posted July 6, 2010 Author Share Posted July 6, 2010 Hey dude this is like i had before it sets same counter to all players not just for me Link to comment
Lordy Posted July 7, 2010 Share Posted July 7, 2010 Alright, in that case I want to know your clientside script. What are you passing as root element in triggerServerEvent Link to comment
Castillo Posted July 7, 2010 Author Share Posted July 7, 2010 Alright, in that case I want to know your clientside script. What are you passing as root element in triggerServerEvent Hmmm a friend already helped me, now i have to test and if dont works i post client part. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now