Jump to content

array problem


Castillo

Recommended Posts

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 :oops:

if someone could help me out i will be very greatfull

Thanks in advance.

Link to comment

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 :D

Link to comment

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

«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 :P), you can convert it once like

local name = tostring(getPlayerName(source))

no need to call tostring() everytime. :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...