Jump to content

[Help] table.remove


Stranger

Recommended Posts

hello everyone, i have this problem which is the first time it happened to me

when i use this code:

local timers = { } 
local created = false 
addCommandHandler ("cre",  
    function () 
        if created == true then return end 
        local x,y,z = getElementPosition (localPlayer) 
        local obj = createObject (3095, x,y,z-1) 
        created = true 
        local timer = setTimer (function () 
        moveObject (obj, 2000, x,y,z+10) 
        end, 1000, 5) 
        table.insert (timers, timer) 
    end 
) 
  
addEventHandler ("onClientPlayerWasted", localPlayer,  
    function () 
        if created == true then 
            for _,v in ipairs (timers) do 
                if isTimer (v) then 
                    killTimer (v) 
                    table.remove (timers, v) 
                end 
            end 
        end 
    end 
) 

this problem shows up to me:

bad argument #2 to 'remove' (number expected, got userdata)

i don't know what is the problem

Link to comment

Table remove function expects a number as argument #2, not user data, in an "ipairs" loop the first member '_' is the index and 'v' the value, that means you can replace line 22 with this:

table.remove (timers, _) 

Also consider changing the variable name '_' to something else as it doesn't look very professional and may fail.

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