Weapon Respawn

I’m not good with entities.
So I want weapons on a map to respawn after someone takes it.
That’s usually done in the map, but the maps I’m using aren’t custom. so I can’t change it now.

A response to my post in the What do you need help with? Thread

I don’t know how to save the position of each weapon in a map :confused:

I think this might be horribly wrong, but this is what I came up with so far:

[lua]
MapWeps={}
MapWeps[“GravGun”]=“weapon_physcannon”
MapWeps[“Crowbar”]=“weapon_crowbar”
MapWeps[“AR2”]=“weapon_ar2”
MapWeps[“Crossbow”]=“weapon_crossbow”
MapWeps[“SLAM”]=“weapon_slam”

function WeaponRespawn()
local spawnpos = table.Random(MapWeps)
local thewep = ents.Create(spawnpos)
local pos = spawnpos:GetPos()
local angle = spawnpos:GetAngle()
GravSet:CallOnRemove()
GravSet:SetPos(pos)
GravSet:SetAngles(angle)
GravSet:Spawn()
end
hook.Add(“InitPostEntity”, “WeaponRespawn”, WeaponRespawn)
timer.Create(“WeaponTimer”,5,0,WeaponRespawn

[/lua]
Error: Hook ‘WeaponRespawn’ Failed: [gamemodes\aderpa\gamemode\init.lua:72] attempt to call method ‘GetPos’ (a nil value)

I don’t think I know what I’m doing.

You are trying to run GetPos() on a string.
table.Random(MapWeps) returns one of the strings you put in, not their entities.

Replace
[lua]
local spawnpos = table.Random(MapWeps)[/lua]

With

[lua]
local spawnpos = table.Random(MapWeps)
local guns = ents.FindByClass(spawnpos)
[/lua]
After the above snippet is run, guns has become a table with all found entities of spawnpos on the map.

Untested but it should work. I’m sure someone will correct it if it doesn’t, otherwise I’ll check it tonight.

[lua]
local function RespawnWeapon(weapon)
timer.Simple(30, function()
local w = ents.Create(weapon:GetClass())
w:SetPos(weapon:GetPos())
w:SetAngles(weapon:GetAngles())
w:Spawn()
w:CallOnRemove(“Respawn”, RespawnWeapon)
end)
end

hook.Add(“InitPostEntity”, “Respawns”, function()
for , ent in pairs(ents.FindByClass("weapon*")) do
ent:CallOnRemove(“Respawn”, RespawnWeapon)
end
end)
[/lua]

Timer Error: [@gamemodes\aderpa\gamemode\init.lua:64] Tried to use a NULL entity!

When you call that function you need to send in the class and position and angles, it’s not going to do much good if you’re removing the weapon and trying to respawn it from it’s current position. It doesn’t exist anymore. :stuck_out_tongue:

[lua]
function RespawnWeapon(weapon,class,pos,angles)
timer.Simple(30, function()
local w = ents.Create(class)
w:SetPos(pos)
w:SetAngles(angles)
w:Spawn()
w:CallOnRemove(“Respawn”, RespawnWeapon, class, pos, angles)
end)
end

hook.Add(“InitPostEntity”, “Respawns”, function()
for , ent in pairs(ents.FindByClass("weapon*")) do
ent:CallOnRemove(“Respawn”, RespawnWeapon, ent:GetClass(), ent:GetPos(), ent:GetAngles())
end
end)
[/lua]

Thanks Drew
Here’s what I have so far
[lua]
function RespawnWeapon(weapon,class,pos,angles)
timer.Create(“TheTimer”,2,1,function(class,pos,angles)
local w = ents.Create(class)
w:SetPos(pos)
w:SetAngles(angles)
w:Spawn()
w:CallOnRemove(“Respawn”, RespawnWeapon, class, pos, angles)
end,class,pos,angles)
end

hook.Add(“InitPostEntity”, “Respawns”, function()
–[[for , ent in pairs(ents.FindByClass("weapon*")) do
ent:CallOnRemove(“Respawn”, RespawnWeapon, ent:GetClass(), ent:GetPos(), ent:GetAngles())
end]]
end)

function CheckForPickup(player,weapon)
RespawnWeapon(weapon,weapon:GetClass(),weapon:GetPos(),weapon:GetAngles())
end
hook.Add(“PlayerCanPickupWeapon”,“CheckForPickup”,CheckForPickup)
[/lua]
It works fine, but it conflicts with my other code that makes it so that players can only pick up one of each type of weapon.

So like, if a player tries to pick up a grav gun after they already have one, they won’t be able to pick it up, but technically they’re supposed to, so the weapon respawn code will make another grav gun respawn, even though one wasn’t taken.

Does anyone know how to fix that?

Other code
[lua]
function GM:PlayerCanPickupWeapon(ply,wep)
if ply:HasWeapon(“weapon_physcannon”)== true then
if wep:GetClass() == “weapon_physcannon” then return false
else return true
end
end
return true
end
[/lua]

You could try simply putting the other code above the command in CheckForPickup like
[lua]
function oneTypeOnly(ply,wep)
if ply:HasWeapon(“weapon_physcannon”)== true then
if wep:GetClass() == “weapon_physcannon” then return false
else return true
end
end
return true
end

function CheckForPickup(player,weapon)
if (oneTypeOnly(player,weapon)) then
RespawnWeapon(weapon,weapon:GetClass(),weapon:GetPos(),weapon:GetAngles())
end
hook.Add(“PlayerCanPickupWeapon”,“CheckForPickup”,CheckForPickup)
[/lua]

Though this is untested you get the gist.(I only put the relevant code in)