Giving the player a table of weapons

Hi all,

In my SWEP I have a function that gets the players weapons ( ply:GetWeapons() )
then I Strip the weapons from them ( ply:StripWeapons() )

Now my problem is, I can’t give the player the weapons back using ply:Give() because the weapons are in a table

How would I go about giving the player the weapons back.

Thanks,
Computer600

Read Neth’s post.

-snip-, ninja’d

Doesn’t Player:GetWeapons() return a table of the weapon entities?

I think he has more of an issue keeping these in the table for later use as his table might get overwriten if someone else gets his weapons stripped.
Using Player as a table holder (or whatever you call it) will be less messy and easier rather than storing a table of players and their stripped weapons.

[lua]
Player.strippedWeapons = ply:GetWeapons() or {}
//do the stripping now
[/lua]
Later, you just have to do:
[lua]
for i, weapon in pairs (Player.strippedWeapons) do
Player:Give( weapon )
end
[/lua]

Thanks for the help,
I’ve tried to implement it in my code and it doesn’t seem to work. Here’s a part of the swep:
The bits that I modified is Player Vars and the taserevive function



function SWEP:tasePlayer(ply, shooter)
	local logtext = shooter:Nick().." tased "..ply:Nick().."
"
	local eyetrace = self.Owner:GetEyeTrace();
	
	if not gamemode.Get("sandbox") then
		if eyetrace.Entity:Team() ==TEAM_ADMIN and self.Owner:IsGov() then
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Not allowed to taze Admins.")
		return end
		if eyetrace.Entity:Team()==TEAM_OWNER and self.Owner:IsGov() then
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Not allowed to taze Server Owner.")
		return end
		if self.Owner:IsGov() and eyetrace.Entity:IsGov() then
			self.Owner:PrintMessage( HUD_PRINTCENTER, "Can't taze government!")
			return
		end
	end
	-- create ragdoll
	local rag = ents.Create( "prop_ragdoll" )
    if not rag:IsValid() then return end
	if not gamemode.Get("sandbox") then
		DB.Log(logtext)
	end
	Msg(logtext)
	ServerLog(logtext)
	--GAMEMODE:PrintMessageAll(HUD_PRINTTALK , shooter:Nick().." tased "..ply:Nick())
	--GAMEMODE:NotifyAll(0, 3, shooter:Nick().." tased "..ply:Nick())
	-- build rag
	rag:SetModel( ply:GetModel() )
    rag:SetKeyValue( "origin", ply:GetPos().x .. " " .. ply:GetPos().y .. " " .. ply:GetPos().z )
	rag:SetAngles(ply:GetAngles())
			
	-- PLAYER VARS
	rag.taseredply = ply
	rag.taseredplyhp = ply:Health()
	rag.taseredplyarmor = ply:Armor()
	rag.taseredplyweapons = ply:GetWeapons() or {}
	table.insert(taseredrags, rag)
		
	-- "remove" player
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	ply:StripWeapons()
	ply:Spectate(OBS_MODE_CHASE)
	ply:SpectateEntity(rag)
	
	-- finalize ragdoll
    rag:Spawn()
    rag:Activate()
	
	-- make ragdoll fall
	rag:GetPhysicsObject():SetVelocity(4*ply:GetVelocity())
	
	-- bring the motherfucker back

    self:setrevivedelay(rag)
	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	
end

function SWEP:taseNPC(npc, npcShooter)
	local logtext = npcShooter:Nick().." tased "..npc:GetClass().."
"
	
	-- get info about npc
	local skin = npc:GetSkin()
	local wep = ""
	local possibleWep = ents.FindInSphere(npc:GetPos(),0.01) -- find anything in the center basically
	local possibleWep = ents.FindInSphere(npc:GetPos(),0.01) -- find anything in the center basically
	if not gamemode.Get("sandbox") then
		DB.Log(logtext)
	end
	Msg(logtext)
	ServerLog(logtext)
	for k, v in pairs(possibleWep) do 
		if string.find(v:GetClass(),"weapon_") == 1 then 
			wep = v:GetClass()
		end
	end

	local citType = "" -- citizen type
	local citMed = 0 -- is it a medic? assume no
	if npc:GetClass() == "npc_citizen" then
		citType = string.sub(npc:GetModel(),21,21) -- get group number (e.g. models/humans/group0#/whatever)
		if string.sub(npc:GetModel(),22,22) == "m" then citMed = 1 end -- medic skins have an "m" after the number
	end

	-- make ragdoll now that all info is gathered	
	local rag = ents.Create( "prop_ragdoll" )
    if not rag:IsValid() then return end
	
	-- build rag
	rag:SetModel( npc:GetModel() )
    rag:SetKeyValue( "origin", npc:GetPos().x .. " " .. npc:GetPos().y .. " " .. npc:GetPos().z )
	rag:SetAngles(npc:GetAngles())
	
	-- npc vars
	rag.tasewasNPC = true
	rag.tasenpcType = npc:GetClass()
	rag.tasenpcWep = wep
	rag.tasenpcCitType = citType
	rag.tasenpcCitMed = citMed
	rag.tasenpcSkin = skin
	rag.tasenpcShooter = npcShooter
	table.insert(taseredrags, rag)
	
	--finalize
	rag:Spawn()
    rag:Activate()
	
	-- make ragdoll fall
  rag:GetPhysicsObject():SetVelocity(8*npc:GetVelocity())
		
	--remove npc
	npc:Remove()

 self:setrevivedelay(rag)

 self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
end

function SWEP:setrevivedelay(rag)
if taseruniquetimer1 > 0 then
taseruniquetimer1 = 10
end
taseruniquetimer1 = taseruniquetimer1 + 1

timer.Create("revivedelay"..taseruniquetimer1, 6, 1, function() self:taserevive(rag); end) ---------------------------------------------
end

function SWEP:taserevive(ent)
	-- revive player
	if !ent then return end
	
	if ent.taseredply then
   if ( !ent.taseredply:IsValid() ) then return end
   local phy = ent:GetPhysicsObject()
	phy:EnableMotion(false)
	ent:SetSolid(SOLID_NONE)
   	ent.taseredply:DrawViewModel(true)
	ent.taseredply:DrawWorldModel(true)
	ent.taseredply:Spawn()
	ent.taseredply:SetHealth(ent.taseredplyhp)
	ent.taseredply:SetArmor(ent.taseredplyarmor)
	print(ent.taseredply)
	print(ent.taseredplyhp)
	print(ent.taseredplyarmor)
	print(ent.taseredplyweapons)
	ent.taseredply:SetPos(ent:GetPos())
	for i, weapon in pairs (ent.taseredplyweapons) do
		ent.taseredply:Give( weapon )
	end
	ent.taseredply:SetVelocity(ent:GetVelocity())


	-- revive npc
	elseif ent.tasewasNPC then
		local npc = ents.Create(ent.tasenpcType) -- create the entity
		
		util.PrecacheModel(ent:GetModel()) -- precache the model
		npc:SetModel(ent:GetModel()) -- and set it
		local spawnPos = ent:GetPos()+Vector(0,0,0) -- position to spawn it
		
		npc:SetPos(spawnPos) -- position
		npc:SetSkin(ent.tasenpcSkin)
		npc:SetAngles(Angle(0,ent:GetAngles().y,0))
		
		if ent.tasenpcWep != "" then -- if it's an NPC and we found a weapon for it when it was spawned, then
			npc:SetKeyValue("additionalequipment",ent.tasenpcWep) -- give it the weapon
		end
		
		if ent.taseentType == "npc_citizen" then
			npc:SetKeyValue("citizentype",ent.tasenpcCitType) -- set the citizen type - rebel, refugee, etc.
			if ent.tasenpcCitType == "3" && ent.tasenpcCitMed==1 then -- if it's a rebel, then it might be a medic, so check that
				npc:SetKeyValue("spawnflags","131072") -- set medic spawn flag
			end
		end
				
		npc:Spawn()
		npc:Activate()
		
cleanup.Add (uplayer, "NPC", npc);

undo.Create ("Tasered NPC");
undo.AddEntity (npc);
undo.SetPlayer (ent.tasenpcShooter);

undo.Finish();

		
	-- don't deal with other ents
	else 
		return
	end
	
		for k, v in pairs(taseredrags) do 
		if v == ent then 
			 table.remove( taseredrags, k ) 
		end
	end
	ent:Remove()

end


I do not know what the problem is

The best debugging way is printing parts or the whole table/variable after using the swep.
Put “PrintTable(rag.taseredplyweapons)” somewhere and check the output in the console.

Also remember that you have to re-give yourself the weapon for the new code to apply (best way is suiciding and giving the weapon back)

Okay in console it prints:




1       =       Weapon [715][keys]
2       =       Weapon [716][weapon_physcannon]
3       =       Weapon [717][gmod_camera]
4       =       Weapon [718][gmod_tool]
5       =       Weapon [719][pocket]
6       =       Weapon [720][weapon_physgun]
7       =       Weapon [723][weapon_keypadchecker]
8       =       Weapon [713][weapon_mad_sg550]



The sg550 is a weapon I gave to my friend before he got tased.
Also when I did this I commented out the give table thing

You sure you are giving weapons back btw. ?

When I give the weapons back it says in console:



[ERROR] addons/taser/lua/weapons/uk_taser/shared.lua:248: bad argument #1 to 'Give' (string expected, got userdata)
  1. Give - [C]:-1
   2. taserevive - addons/taser/lua/weapons/uk_taser/shared.lua:248
    3. unknown - addons/taser/lua/weapons/uk_taser/shared.lua:225



and I added print(weapon) before this and it returned:



table: 0xa5f92530
Weapon [NULL]


Eh, should’ve paid more attention.
Heres the fix:
It’s userdata so :GetWeapons() outputs table of entities, all you have to do is change
[lua]Player:Give(weapon)[/lua]
to
[lua]Player:Give(weapon:GetClass())[/lua]

Thanks, I actually thought that would fix it,
but I still get the this error now: -_-




[ERROR] addons/taser/lua/weapons/uk_taser/shared.lua:248: Tried to use a NULL entity!
  1. GetClass - [C]:-1
   2. taserevive - addons/taser/lua/weapons/uk_taser/shared.lua:248
    3. unknown - addons/taser/lua/weapons/uk_taser/shared.lua:225



Also I edited the print to print(weapon:GetClass()) as well to see if it worked and it returned nothing, except that table id thing: table:0xa8d5f190
I may have edited something while I was waiting for a reply here’s my full code again:



function SWEP:tasePlayer(ply, shooter)
	local logtext = shooter:Nick().." tased "..ply:Nick().."
"
	local eyetrace = self.Owner:GetEyeTrace();
	
	if not gamemode.Get("sandbox") then
		if eyetrace.Entity:Team() ==TEAM_ADMIN and self.Owner:IsGov() then
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Not allowed to taze Admins.")
		return end
		if eyetrace.Entity:Team()==TEAM_OWNER and self.Owner:IsGov() then
		self.Owner:PrintMessage( HUD_PRINTCENTER, "Not allowed to taze Server Owner.")
		return end
		if self.Owner:IsGov() and eyetrace.Entity:IsGov() then
			self.Owner:PrintMessage( HUD_PRINTCENTER, "Can't taze government!")
			return
		end
	end
	-- create ragdoll
	local rag = ents.Create( "prop_ragdoll" )
    if not rag:IsValid() then return end
	if not gamemode.Get("sandbox") then
		DB.Log(logtext)
	end
	Msg(logtext)
	ServerLog(logtext)
	--GAMEMODE:PrintMessageAll(HUD_PRINTTALK , shooter:Nick().." tased "..ply:Nick())
	--GAMEMODE:NotifyAll(0, 3, shooter:Nick().." tased "..ply:Nick())
	-- build rag
	rag:SetModel( ply:GetModel() )
    rag:SetKeyValue( "origin", ply:GetPos().x .. " " .. ply:GetPos().y .. " " .. ply:GetPos().z )
	rag:SetAngles(ply:GetAngles())
			
	-- player vars
	rag.taseredply = ply
	rag.taseredplyhp = ply:Health()
	rag.taseredplyarmor = ply:Armor()
	rag.taseredplyweapons = ply:GetWeapons() or {}
	table.insert(taseredrags, rag)
		
	-- "remove" player
	PrintTable(rag.taseredplyweapons)
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	ply:StripWeapons()
	ply:Spectate(OBS_MODE_CHASE)
	ply:SpectateEntity(rag)
	
	-- finalize ragdoll
    rag:Spawn()
    rag:Activate()
	
	-- make ragdoll fall
	rag:GetPhysicsObject():SetVelocity(4*ply:GetVelocity())
	
	-- bring the motherfucker back

    self:setrevivedelay(rag)
	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	
end

function SWEP:taseNPC(npc, npcShooter)
	local logtext = npcShooter:Nick().." tased "..npc:GetClass().."
"
	
	-- get info about npc
	local skin = npc:GetSkin()
	local wep = ""
	local possibleWep = ents.FindInSphere(npc:GetPos(),0.01) -- find anything in the center basically
	local possibleWep = ents.FindInSphere(npc:GetPos(),0.01) -- find anything in the center basically
	if not gamemode.Get("sandbox") then
		DB.Log(logtext)
	end
	Msg(logtext)
	ServerLog(logtext)
	for k, v in pairs(possibleWep) do 
		if string.find(v:GetClass(),"weapon_") == 1 then 
			wep = v:GetClass()
		end
	end

	local citType = "" -- citizen type
	local citMed = 0 -- is it a medic? assume no
	if npc:GetClass() == "npc_citizen" then
		citType = string.sub(npc:GetModel(),21,21) -- get group number (e.g. models/humans/group0#/whatever)
		if string.sub(npc:GetModel(),22,22) == "m" then citMed = 1 end -- medic skins have an "m" after the number
	end

	-- make ragdoll now that all info is gathered	
	local rag = ents.Create( "prop_ragdoll" )
    if not rag:IsValid() then return end
	
	-- build rag
	rag:SetModel( npc:GetModel() )
    rag:SetKeyValue( "origin", npc:GetPos().x .. " " .. npc:GetPos().y .. " " .. npc:GetPos().z )
	rag:SetAngles(npc:GetAngles())
	
	-- npc vars
	rag.tasewasNPC = true
	rag.tasenpcType = npc:GetClass()
	rag.tasenpcWep = wep
	rag.tasenpcCitType = citType
	rag.tasenpcCitMed = citMed
	rag.tasenpcSkin = skin
	rag.tasenpcShooter = npcShooter
	table.insert(taseredrags, rag)
	
	--finalize
	rag:Spawn()
    rag:Activate()
	
	-- make ragdoll fall
  rag:GetPhysicsObject():SetVelocity(8*npc:GetVelocity())
		
	--remove npc
	npc:Remove()

 self:setrevivedelay(rag)

 self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
end

function SWEP:setrevivedelay(rag)
if taseruniquetimer1 > 0 then
taseruniquetimer1 = 10
end
taseruniquetimer1 = taseruniquetimer1 + 1

timer.Create("revivedelay"..taseruniquetimer1, 6, 1, function() self:taserevive(rag); end) ---------------------------------------------
end

function SWEP:taserevive(ent)
	-- revive player
	if !ent then return end
	
	if ent.taseredply then
   if ( !ent.taseredply:IsValid() ) then return end
   local phy = ent:GetPhysicsObject()
	phy:EnableMotion(false)
	ent:SetSolid(SOLID_NONE)
   	ent.taseredply:DrawViewModel(true)
	ent.taseredply:DrawWorldModel(true)
	ent.taseredply:Spawn()
	ent.taseredply:SetHealth(ent.taseredplyhp)
	ent.taseredply:SetArmor(ent.taseredplyarmor)
	print(ent.taseredply)
	print(ent.taseredplyhp)
	print(ent.taseredplyarmor)
	print(ent.taseredplyweapons)
	ent.taseredply:SetPos(ent:GetPos())
	for i, weapon in pairs (ent.taseredplyweapons) do
		ent.taseredply:Give( weapon:GetClass() )
	end

	ent.taseredply:SetVelocity(ent:GetVelocity())


	-- revive npc
	elseif ent.tasewasNPC then
		local npc = ents.Create(ent.tasenpcType) -- create the entity
		
		util.PrecacheModel(ent:GetModel()) -- precache the model
		npc:SetModel(ent:GetModel()) -- and set it
		local spawnPos = ent:GetPos()+Vector(0,0,0) -- position to spawn it
		
		npc:SetPos(spawnPos) -- position
		npc:SetSkin(ent.tasenpcSkin)
		npc:SetAngles(Angle(0,ent:GetAngles().y,0))
		
		if ent.tasenpcWep != "" then -- if it's an NPC and we found a weapon for it when it was spawned, then
			npc:SetKeyValue("additionalequipment",ent.tasenpcWep) -- give it the weapon
		end
		
		if ent.taseentType == "npc_citizen" then
			npc:SetKeyValue("citizentype",ent.tasenpcCitType) -- set the citizen type - rebel, refugee, etc.
			if ent.tasenpcCitType == "3" && ent.tasenpcCitMed==1 then -- if it's a rebel, then it might be a medic, so check that
				npc:SetKeyValue("spawnflags","131072") -- set medic spawn flag
			end
		end
				
		npc:Spawn()
		npc:Activate()
		
cleanup.Add (uplayer, "NPC", npc);

undo.Create ("Tasered NPC");
undo.AddEntity (npc);
undo.SetPlayer (ent.tasenpcShooter);

undo.Finish();

		
	-- don't deal with other ents
	else 
		return
	end
	
		for k, v in pairs(taseredrags) do 
		if v == ent then 
			 table.remove( taseredrags, k ) 
		end
	end
	ent:Remove()

end


Probably because you’re trying to get the class of an entity that no longer exists. Instead of saving Player:GetWeapons() you should loop through that table of entities and insert the classes into a table.
When giving back weapons, you loop through your created table and Player:Give(TableValue)

How would I go about doing this ?

Thanks,
Computer600

[lua]
for index, value in pairs(ply:GetWeapons()) do
table.insert(ply.StrippedWeapons, value:GetClass())
end
[/lua]

Where index will be, well the index and value will be the weapon entity.
index, value are often replaced with k, v but can be replaced by anything.
The two values are those you use inside the loop.
So here you could do table.insert(ply.StrippedWeapons, value:GetClass())
You’ll have to define ply.StrippedWeapons as a table first.