• SWEP Help
    4 replies, posted
I'm trying to get this delay working, so that if it's succesful, or vice versa it takes time to cooldown and recharge before it can be used again, can somebody help me out here? Thanks. [code] if SERVER then AddCSLuaFile( "shared.lua" ) end if CLIENT then SWEP.PrintName = "Cry out" SWEP.Author = "Pancakes" SWEP.Slot = 1 SWEP.SlotPos = 1 SWEP.IconLetter = "b" end function SWEP:Initialize() self:SetNWBool("cooled", true) end function SWEP:PrimaryAttack(ply) self:EmitSound("ba_helpmeout.wav") resu=math.random(1,10) cooled=self:GetNWBool("cooled") touchPoint=self.Owner:GetEyeTrace() if(resu>=8 and cooled==true) then self:SetNWBool("cooled", false) newSurvivor=ents.Create("npc_citizen") newSurvivor:SetPos(touchPoint.HitPos) newSurvivor:SetNWString("master","") newSurvivor:Activate() newSurvivor:Spawn() timer.Simple( 120, self:SetNWBool("cooled", true)) end if(resu<=7 and cooled==true) then self:SetNWBool("cooled", false) newZomb=ents.Create("npc_zombie") newZomb:SetPos(touchPoint.HitPos) newZomb:SetNWString("master","") newZomb:Activate() newZomb:Spawn() timer.Simple( 10, self:SetNWBool("cooled", true)) end if(cooled==false) then Msg("Don't want more trouble...") end end SWEP.Base = "weapons_base" SWEP.Spawnable = true SWEP.AdminSpawnable = true [/code]
[QUOTE=TheNerdPest14;22593532]I'm trying to get this delay working, so that if it's succesful, or vice versa it takes time to cooldown and recharge before it can be used again, can somebody help me out here? Thanks. [/QUOTE] Allow me to clean than up for you. [lua] if SERVER then AddCSLuaFile( "shared.lua" ) end if CLIENT then SWEP.PrintName = "Cry out" SWEP.Author = "Pancakes" SWEP.Slot = 1 SWEP.SlotPos = 1 SWEP.IconLetter = "b" end function SWEP:Initialize() self:SetNWBool( "cooled", true ) end local resu, cooled, touchPoint, newSurvivor, newZomb function SWEP:PrimaryAttack() self:EmitSound( "ba_helpmeout.wav" ) resu = math.random( 1, 10 ) cooled = self:GetNWBool( "cooled" ) touchPoint = self.Owner:GetEyeTrace() if ( resu >= 8 and cooled == true ) then self:SetNWBool( "cooled", false ) newSurvivor = ents.Create( "npc_citizen" ) newSurvivor:SetPos( touchPoint.HitPos ) newSurvivor:SetNWString( "master", "" ) newSurvivor:Activate() newSurvivor:Spawn() timer.Simple( 120, function() self:SetNWBool( "cooled", true ) end ) end if ( resu <= 7 and cooled == true ) then self:SetNWBool( "cooled", false ) newZomb=ents.Create("npc_zombie") newZomb:SetPos(touchPoint.HitPos) newZomb:SetNWString("master","") newZomb:Activate() newZomb:Spawn() timer.Simple( 10, function() self:SetNWBool("cooled", true) end ) end if ( cooled == false ) then Msg( "Don't want more trouble...\n" ) end end SWEP.Base = "weapons_base" SWEP.Spawnable = true SWEP.AdminSpawnable = true [/lua] 1. Learn how local vs. global variables work. I took the liberty of declaring all of your locals outside of the function as an example of good practice. In this situation, it's good since there aren't any other opportunities for conflict. 2. Your timers were set up wrong. There are two forms. Read about [b][url=http://wiki.garrysmod.com/?title=Timer.Simple]Timer.Simple [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] in the wiki. 3. I added a lot of spaces and used [noparse][lua][/lua][/noparse] tags for neatness. Much easier to read. 4. SWEP methods do not take the player as an argument. Use self.Owner instead. Someone correct me if I missed anything, but this should work.
Thank you... >_<
I wasn't trying to be mean, just pointing out the errors so you can learn from them. :smile:
[QUOTE=grea$emonkey;22605051]I wasn't trying to be mean, just pointing out the errors so you can learn from them. :smile:[/QUOTE] I'm sorry. >_< But thank you so much. :) I appreciate the kindness, without the attempted retort. :/ That's not something I see too much in my life, so thanks again.
Sorry, you need to Log In to post a reply to this thread.