Help with Tables!

This is a ULX command im working on that should give back a players weapons from before they died. if they by example got rdmed or something.
But i need help to make it save the weapons they had before they died into a table


hook.Add("PlayerDeath", "ReturnWepHook", function ulx.returnwep(calling_ply, target_ply)
	
    ulx.fancyLogAdmin( calling_ply, "#A returned weapons to #T", target_ply)
end
local returnwep = ulx.command(CATEGORY_NAME, "ulx returnwep", ulx.returnwep, "!returnwep",true)
returnwep:defaultAccess( ULib.ACCESS_ADMIN )
returnwep:help( "Returns a players weapon" )

This looks very wrong.

Create the player death hook and, inside it, make a player variable that stores all of their weapons (ply.lastweapons = ply:GetWeapons())

Then create a ulx command that simply loops through the players weapons and give them each weapon.

not sure im understand how can you do it? BTW im very new for lua



local tab = {}  // table which will contain player weapons

hook.Add("PlayerDeath", "ReturnWepHook", function(ply)

for i = 1 , #ply:GetWeapons() do 
table.Insert(ply:GetWeapons()*:GetClass(),tab) // inserts classname to table
end



end)




 

:frowning: I wanna be home coding for myself, but having an hour left of work I suppose this will have to quench that need.

You can make this even simpler:
THIS MAY NOT WORK



 -- Table 'weapon_store' to hold each players weapons on death
local weapon_store = {}

-- When the player dies...
hook.Add("PlayerDeath", "ReturnWepHook", function( pl )

    -- Store the players weapons in the weapon_store table using the player object as the key and their weapons table as the value
    weapon_store[ pl ] = pl:GetWeapons()
end)


THIS ONE WILL



 -- Table 'weapon_store' to hold each players weapons on death
local weapon_store = {}

-- When the player dies...
hook.Add("PlayerDeath", "ReturnWepHook", function( pl )

    -- make sure it exists and empty it if it does
    weapon_store[ pl ] = {}

    -- Loop through weapons and add class name to players weapon store.
    for _, weapon in pairs( pl:GetWeapons() ) do
        table.insert( weapon_store[ pl ], weapon:GetClass() )
    end

end)


weapon_store[PLAYER] will then equal a table of the weapons they had on death.

So when you use the command ( don’t know ULX so I’ll just make it give them back their weapons when they spawn )




-- When the player spawns...
hook.Add( "PlayerSpawn", "GiveBackWeapons", function( pl ) -- Use the content of this function in your ULX command function

    -- Loop through the weapons_store[ pl ] to get their old weapons
    for _, weapon in pairs( weapon_store[ pl ] ) do

        -- For each weapon in the store, give it back to the player.
        pl:GiveWeapon( weapon:GetClass() )

    end

    -- Remove the weapon store table for that player
    weapon_store[ pl ] = nil

end )

You’ll also want to check if weapon_store[ pl ] exists before looping through it.

At work, so above may be wrong - don’t have time to double check but hopefully you get the idea.

Alternatively, if you wanted to filter weapons out - you could do:



 -- Table 'weapon_store' to hold each players weapons on death
local weapon_store = {}

 -- Table 'weapon_store_filter' - if weapon is set to true, it will be saved on death
local weapon_store_filter = {

    -- The player's crowbar will be saved in this command
    ['weapon_crowbar'] = true

}

-- When the player dies...
hook.Add("PlayerDeath", "ReturnWepHook", function( pl )

    -- Make sure their table is empty from the previous death
    weapon_store[ pl ] = {}

    -- For each of their weapons
    for _, weapon in pairs( pl:GetWeapons() ) do

        -- If the weapon is in the filter table, then include it        
        if weapon_store_filter[weapon:GetClass()] then

            -- Insert it into their weapon_store
            table.insert( weapon_store[pl], weapon )

        end

    end

end)


store class names instead, because the weapon entities prob won’t be valid when you want to give him his stuff back.

you also don’t need a lookup like that:
[lua]hook.Add(“PlayerDeath”, “StoreWeapons”, function(ply)
local t = {}
for k, v in pairs(ply:GetWeapons()) do
t[#t + 1] = v:GetClass()
end
ply.StoredWeapons = t
end)[/lua]

Fair point - so using my above second example you may need to do a similar loop and store the GetClass name in the table instead of the entity.

Adjusted my previous answer to echo you’re advice.

Which one of these post should i use im being confused LOL XD

Use the one Luni posted.

Or you can always try both and see what works out best :slight_smile:

Both, I only added a couple more examples into mine as to different ways you could do the weapon storing with more customization.

Luni shows you a good way to store the weapons - taking into account the weapon class name rather then the object. My middle part shows you roughly how to retrieve it on spawn.