opnaiC

[HELP] Doesnt delete all rows from table

Recommended Posts

local vehicles = {}

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
            for i, v in ipairs (vehicles) do
			    local player, veh = unpack(v)
                    if (player == thePlayer) then
                        destroyElement (veh)
						table.remove (vehicles, i)
						return
                    end					
			end						
		end
	end
)

Hi, I want the script to delete all vehicles and rows, but it only deletes the last one, why  ?

Edited by opnaiC

Share this post


Link to post

unless you're going to use keys then I don't see why you're using a key loop.

local vehicles = {}

addCommandHandler('deveh',function(player)
	for i=1,#vehicles do
		local plr,veh = unpack(vehicles[i])
		if (plr == player) then
			destroyElement(veh)
			table.remove(vehicles,i)
		end						
	end
end)

 

Share this post


Link to post
10 minutes ago, ShayF said:

unless you're going to use keys then I don't see why you're using a key loop.


local vehicles = {}

addCommandHandler('deveh',function(player)
	for i=1,#vehicles do
		local plr,veh = unpack(vehicles[i])
		if (plr == player) then
			destroyElement(veh)
			table.remove(vehicles,i)
		end						
	end
end)

 

Sorry I posted the wrong command. I have my own chat so I dont need the commandHandler. So basically I want all cars to disappear that the player spawned that left.

addEventHandler ("onPlayerQuit", getRootElement(),
    function ()
     for i=1,#vehicles do
		local player, veh = unpack(vehicles[i])
		if (source == player) then
			destroyElement(veh)
			table.remove(vehicles, i)
		end						
	end		
    end
)

Wg13H5n.png

Share this post


Link to post

You are passing a nil to the unpack function. Basically, this:

local player, veh = unpack(nil)

vehicles [ i ] does not contain a table, so you get nil instead when you try accessing it. You need to show us the code where you insert tables into vehicles table.

Edited by Saml1er

Share this post


Link to post
2 hours ago, Saml1er said:

You are passing a nil to the unpack function. Basically, this:


local player, veh = unpack(nil)

vehicles [ i ] does not contain a table, so you get nil instead when you try accessing it. You need to show us the code where you insert tables into vehicles table.

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command, model)
	local serverTime = getRealTime()
    local time = string.format("[%02d:%02d:%02d]", serverTime.hour, serverTime.minute, serverTime.second)
		if(command == "/cveh") then
            if tonumber(model) then
			    local x, y, z = getElementPosition (thePlayer)
                local veh = createVehicle (tonumber(model), x, y, z, 0, 0, 0, "ADMIN")
				table.insert (vehicles, 1, {thePlayer, veh})
				setElementPosition (thePlayer, x, y, z +2)
				return
			else
                sendMessageToPlayer ("#ce3737"..time.." Формат команды: '/cveh [model]'", thePlayer)
                return				
            end			
		end
	local time = nil	
	end
)

Fully working command.

If I want now to delete the las spawned vehicle I use this and it works:

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
            for i, v in ipairs (vehicles) do
			    local player, veh = unpack(v)
                    if (player == thePlayer) then
                        destroyElement (veh)
						table.remove (vehicles, i)
						return
                    end					
			end			
		end
	end
)

But now I want a function where all vehicles that a certain player spawned get deleted when he leave the game.

Share this post


Link to post
local vehicles = {}
addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command, model)
		local serverTime = getRealTime()
		local time = string.format("[%02d:%02d:%02d]", serverTime.hour, serverTime.minute, serverTime.second)
		if(command == "/cveh") and tonumber(model) then
			if not vehicles[thePlayer] then
				vehicles[thePlayer] = {}
			end
			local x, y, z = getElementPosition (thePlayer)
			local veh = createVehicle (tonumber(model), x, y, z, 0, 0, 0, "ADMIN")
			table.insert (vehicles[thePlayer], veh)
			
			setElementPosition (thePlayer, x, y, z +2)
			return
		else
			sendMessageToPlayer ("#ce3737"..time.." Формат команды: '/cveh [model]'", thePlayer)
			return				
		end			
	end
)

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
			local vehs = vehicles[thePlayer]
			if vehs and type(vehs) == "table" and #vehs > 0 then
				destroyElement( vehs[#vehs] )
			end			
		end
	end
)

Removing all vehicles:

addEventHandler( "onPlayerQuit", root,
	function()
		if vehicles[source] then
			for i, veh in ipairs( vehicles[source] ) do
				destroyElement( veh )
				vehicles[source] = nil
			end
		end	
	end
)

 

Share this post


Link to post
2 hours ago, DNL291 said:

local vehicles = {}
addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command, model)
		local serverTime = getRealTime()
		local time = string.format("[%02d:%02d:%02d]", serverTime.hour, serverTime.minute, serverTime.second)
		if(command == "/cveh") and tonumber(model) then
			if not vehicles[thePlayer] then
				vehicles[thePlayer] = {}
			end
			local x, y, z = getElementPosition (thePlayer)
			local veh = createVehicle (tonumber(model), x, y, z, 0, 0, 0, "ADMIN")
			table.insert (vehicles[thePlayer], veh)
			
			setElementPosition (thePlayer, x, y, z +2)
			return
		else
			sendMessageToPlayer ("#ce3737"..time.." Формат команды: '/cveh [model]'", thePlayer)
			return				
		end			
	end
)

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
			local vehs = vehicles[thePlayer]
			if vehs and type(vehs) == "table" and #vehs > 0 then
				destroyElement( vehs[#vehs] )
			end			
		end
	end
)

Removing all vehicles:


addEventHandler( "onPlayerQuit", root,
	function()
		if vehicles[source] then
			for i, veh in ipairs( vehicles[source] ) do
				destroyElement( veh )
				vehicles[source] = nil
			end
		end	
	end
)

 

After using ur script my chatbox start buging, the script is also not working complety. The /dveh command is not working.

 

Share this post


Link to post

Well, I have no idea what's going on with your chat.

Try replacing

destroyElement( vehs[#vehs] )

with:

destroyElement( vehicles[thePlayer][#vehs] )

On the command /dveh.

I had put 'vehicles[source] = nil' inside the loop, so use this:

addEventHandler( "onPlayerQuit", root,
	function()
		if vehicles[source] then
			for i, veh in ipairs( vehicles[source] ) do
				destroyElement( veh )
			end
			vehicles[source] = nil
		end	
	end
)

 

Edited by DNL291

Share this post


Link to post
1 hour ago, DNL291 said:

Well, I have no idea what's going on with your chat.

Try replacing


destroyElement( vehs[#vehs] )

with:


destroyElement( vehicles[thePlayer][#vehs] )

On the command /dveh.

I had put 'vehicles[source] = nil' inside the loop, so use this:


addEventHandler( "onPlayerQuit", root,
	function()
		if vehicles[source] then
			for i, veh in ipairs( vehicles[source] ) do
				destroyElement( veh )
			end
			vehicles[source] = nil
		end	
	end
)

 

So look:

Now the spawning cmd is working and also all vehicles disappear when the player leaves the game.

When I use the /dveh cmd, the last vehicle that I spawned got delete. But when I use the cmd again the next last spawned vehicle doesnt disappear.

This is the error (line 300 is the line with destroyElement in the /dveh cmd)

XQmE8TY.png

 

(Why we are not removing the vehicle from the table that got deleted ?)

Edited by opnaiC

Share this post


Link to post
addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
			local vehs = vehicles[thePlayer]
			if vehs and type(vehs) == "table" and #vehs > 0 then
				destroyElement( vehicles[thePlayer][#vehs] )
				vehicles[thePlayer][#vehs] = nil
			end			
		end
	end
)

Try it.

Share this post


Link to post
14 hours ago, DNL291 said:

addEventHandler("cmdHandler",getRootElement(), 
	function(thePlayer, command)
		if(command == "/dveh") then
			local vehs = vehicles[thePlayer]
			if vehs and type(vehs) == "table" and #vehs > 0 then
				destroyElement( vehicles[thePlayer][#vehs] )
				vehicles[thePlayer][#vehs] = nil
			end			
		end
	end
)

Try it.

Thank you its working now.

Share this post


Link to post

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.