• Respawn Weapons on Pickup or Timer
    12 replies, posted
Hello, So I'm fiddling around with LUA for the first time and I wish some people could help me a bit. I would like to respawn weapons that are placed in the map when you pick them up (this can be with a small delay), just like HL2:DM. Now I found out that I can make everything respawn with this code placed in the shared.lua [CODE]timer.Create( "my_timer", 10, 0, function() game.CleanUpMap(); end)[/CODE] The problem is all of the physics props get respawned which is normal, but not what I want. I also found a piece of code here: [url]https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexe352.html[/url] I tried this but it doesn't seem to work. [CODE] local nextclass, nextpos, nextang local function RespawnTrigger(ply,wep) nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() wep:Respawn() return(true) end hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", RespawnTrigger) hook.Add("OnEntityCreated","RespawnManagement",function(ent) if ent:IsWeapon() then local function DelayedRespawn(ent,class,model) if !(ent:GetClass() == class ) then return end nextclass = "" ent:SetPos(nextpos) ent:SetAngles(nextang) end timer.Simple(0.1,DelayedRespawn, ent,nextclass) -- The timer is needed so that the entity's class and model is properly defined. end end) [/CODE] It would be used for HL2 weapons like physcannon, grenades, ... Is there a way to make the CleanUpMap(); only work for weapons? Or how do I target only weapons? Thanks Michael [B]SOLUTION BY [URL="http://facepunch.com/member.php?u=337048"]KULCRIS[/URL][/B] [CODE] hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() end) end) function GM:PlayerCanPickupWeapon(ply, wep) if ply:HasWeapon("weapon_physcannon") then return false end return true end [/CODE]
You can just simply create a timer when player picks up the weapon like this [lua] hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() timer.Simple(0.1,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() end) end) [/lua]
You sir, are a generous. Thanks a lot! [editline]15th March 2015[/editline] [QUOTE=Masster;47326918]You can just simply create a timer when player picks up the weapon like this [lua] hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() timer.Simple(0.1,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() end) end) [/lua][/QUOTE] Hey Masster, Just a quick question, it's all working but for some reason it sometimes freaks out and duplicates the picked up weapons multiple times and makes the server crash. [IMG]http://i.imgur.com/bYoe7O7.png[/IMG] Is there a way to freeze the spawned weapons in place and not shootable. Or is it possible so you can't pick up a physcannon when you already have one? Thanks for your help.
[QUOTE=PoisonMichael;47327026]You sir, are a generous. Thanks a lot! [editline]15th March 2015[/editline] Hey Masster, Just a quick question, it's all working but for some reason it sometimes freaks out and duplicates the picked up weapons multiple times and makes the server crash. [IMG]http://i.imgur.com/bYoe7O7.png[/IMG] Is there a way to freeze the spawned weapons in place and not shootable. Or is it possible so you can't pick up a physcannon when you already have one? Thanks for your help.[/QUOTE] ent:EnableMotion(false) [editline]15th March 2015[/editline] [url]http://wiki.garrysmod.com/page/GM/PlayerCanPickupWeapon[/url] [editline]15th March 2015[/editline] GM:PlayerCanPickupWeapon( ply, wep ) if ply:HasWeapon( wep ) then return false else return true end i believe this will help? im not sure though if this is what you are looking for
[QUOTE=kulcris;47327381]ent:EnableMotion(false) GM:PlayerCanPickupWeapon( ply, wep ) if ply:HasWeapon( wep ) then return false else return true end i believe this will help? im not sure though if this is what you are looking for[/QUOTE] Hey Kulcris, I tried some stuff and your code is working, however now it's completely out of control lol. I use this code atm: [CODE] hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() end) end) function GM:PlayerCanPickupWeapon(ply, wep) if ply:HasWeapon("weapon_physcannon") then return false end return true end [/CODE] When I pick up a physcannon it gives me 1 and the rest stays at it's place. However a few seconds later (2 seconds I guess since thats whats in the code) everything respawns where I walked over. Video: [URL="https://www.youtube.com/watch?v=Rb5Bi1F54YQ"]https://www.youtube.com/watch?v=Rb5Bi1F54YQ[/URL]
ROFL thats pretty awesome honestly... now you didnt add ent:EnableMotion(false) so that would be why they go flying ... but hmmm [editline]15th March 2015[/editline] also you put false end return true it should be else return true end [editline]15th March 2015[/editline] else return true end end (to end the function) [editline]15th March 2015[/editline] [lua]hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() if ply:HasWeapon("weapon_physcannon") then return false timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() ent:EnableMotion(false) end else return true end) end) [/lua] Try this [editline]15th March 2015[/editline] you have it respawning no mater if someone picks it up or not, have them only respawn if they are allowed to pick up [editline]15th March 2015[/editline] let me know if this works please it will allow for some easy to create respawning weapons lol
[QUOTE=kulcris;47327765] [lua]hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() if ply:HasWeapon("weapon_physcannon") then return false timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() ent:EnableMotion(false) end else return true end) end) [/lua] [/QUOTE] So I added this to init.lua but it doesn't seem to do a lot :o Or do I have to add it somewhere else?
ummmm just add it to a lua file in lua/autorun [editline]15th March 2015[/editline] sorry ! dont do it! lol one moment i mixed something up [editline]15th March 2015[/editline] [code]hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() if wep:GetClass() == "weapon_physcannon" and ply:HasWeapon("weapon_physcannon") then return false else timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() ent:EnableMotion(false) end) return true end end)[/code] [editline]15th March 2015[/editline] the wep:GetClass() == "weapon_physcannon" will make it only work if the weapon you are trying to pick up is not a physcannon but you can pick up multiple of other guns before i had it set to do exactly the same thing it was doing before except it wouldnt respawn if you did pick it up XD
[QUOTE=kulcris;47328048] [code]hook.Add("PlayerCanPickupWeapon", "RespawnTrigger", function(ply,wep) local nextclass, nextpos, nextang = wep:GetClass(), wep:GetPos(), wep:GetAngles() if wep:GetClass() == "weapon_physcannon" and ply:HasWeapon("weapon_physcannon") then return false else timer.Simple(5,function() local ent = ents.Create(nextclass) ent:SetPos( nextpos ) ent:SetAngles( nextang ) ent:Spawn() ent:EnableMotion(false) end) return true end end)[/code] [/QUOTE] This seems to work perfectly! I can pickup 1 weapon and not everything at once :D Now only one little thing. I can still shoot and pickup (right click) the weapons. I guess that ent:EnableMotion(false) doesn't work?
hmmmmm guess not, try this ent:Sleep() in place of ent:enabledmotion(false)
[QUOTE=kulcris;47328206]hmmmmm guess not, try this ent:Sleep() in place of ent:enabledmotion(false)[/QUOTE] Hmm, no I can still do it. But don't worry, it's ok. I'm happy you got the respawn to work. Thanks a lot!
or you can try ent:SetCollisionGroup(COLLISION_GROUP_DEBRIS) though i dont know if you will be able to pick it up still [editline]15th March 2015[/editline] other then that i have no more ideas XD
[QUOTE=kulcris;47328238]or you can try ent:SetCollisionGroup(COLLISION_GROUP_DEBRIS) though i dont know if you will be able to pick it up still [editline]15th March 2015[/editline] other then that i have no more ideas XD[/QUOTE] Doesn't do a lot :p But thanks!
Sorry, you need to Log In to post a reply to this thread.