Castillo Posted December 17, 2010 Share Posted December 17, 2010 hi again, i've got a problem and i can't get how to fix it, vehicle = {} function spawn_car (ID) local name = getPlayerName(source) vehicle[tostring(name)] = {} local root = getResourceConfig ("data.xml") local index = getCarIndex (getAccountName(getPlayerAccount(source)),root) if (index) then local accountRo = xmlFindChild (root,"user",index) for _aux=0, #xmlNodeGetChildren(accountRo)-1 do local accountRooo = xmlFindChild (accountRo,"car",_aux) if (tostring(xmlNodeGetAttribute(accountRooo,"id")) == ID) then if isElement(vehicle[tostring(name)][tonumber(ID)]) then destroyElement(vehicle[tostring(name)][tonumber(ID)]) end x,y,z = getElementPosition(source) local cars = xmlNodeGetAttributes (accountRooo) vehicle[tostring(name)][tonumber(ID)] = createVehicle(tonumber(cars["name"]),x,y+5,z) outputChatBox ( "Vehicle: '" .. tonumber(ID) .. "' spawned!", source, 0, 255, 0 ) end end end end addEvent( "spawn_car", true ) addEventHandler( "spawn_car", getRootElement(),spawn_car) this code its suposed to check if the player has already a vehicle with that ID and if he does destroy it, my problem is that i can spawn infinite vehicles with same ID if someone could help me out i will be very greatfull Thanks in advance. Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 vehicle[tostring(name)] = {} you're emptying the player's vehicle table on each spawn_car call. so vehicle[tostring(name)][tonumber(ID)] is always nil, therefore vehicle is never destoyed. Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 oh lol, i suposed that when i was writting it but a friend told me was fine edit: thanks its solved, i added when the player joins create it Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 quick way to fix this: if not vehicle[tostring(name)] or type(vehicle[tostring(name)]) ~= "table" then vehicle[tostring(name)] = {} end but it's best to create/destroy all player-related tables and stuuf when he joins/leaves, imo. EDIT: damn this post editing Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 i've got another problem, i want to destroy his vehicles when quit but dunno why it won't work and gives me a error. addEventHandler( "onPlayerQuit", getRootElement(), function () local name = getPlayerName(source) if vehicle[tostring(name)] then for i,v in pairs(vehicle[tostring(name)]) do if isElement(vehicle[tostring(name)][v]) then destroyElement(vehicle[tostring(name)][v]) end end end vehicle[tostring(name)] = nil end) error: attempt to index field '?' (a nil value) Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 «i» is an index/key of a value in a table and «v» is the value itself. so either use: if isElement(vehicle[tostring(name)][i]) then destroyElement(vehicle[tostring(name)][i]) end or just v: if isElement(v) then destroyElement(v) end and if you're so afraid that player's name wont be a string (although getPlayerName returns string ), you can convert it once like local name = tostring(getPlayerName(source)) no need to call tostring() everytime. Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 oh i didn't know that , thanks for the help again. 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