• Limit amount of times for a player to use a SWEP.
    27 replies, posted
Hello, I'm trying to limit the amount of times the player can regenerate. (SecondaryAttack) this is my code: regenerations = 2 function SWEP:SecondaryAttack() -- Fast Regeneration     regenerated = false     if self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") != nil then         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_Head1") )         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_L_Foot") )         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") )         end     if SERVER then         self.Owner:Freeze(true)         self.Owner:GodEnable()     end     for k, v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound02) end     timer.Simple(1, function()     local model = table.Random(regen.models)     self.Owner:SetModel( model )     self.Owner:SetModelName( model )     timer.Simple(1, function()     self.Owner:Freeze(false)     self.Owner:SetHealth(regen.FastRegenHealth)     self.Owner:StopParticles()     lastregen = regen.models     regenerated = true     regenerations = -1     timer.Simple(5, function()     if SERVER then         self.Owner:GodDisable()     end     timer.Create("regencyclefast",600,4,function()         if regenerated == true then             for k,v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound03) end             self.Owner:SetHealth(self.Owner:Health()+500)         end     end) end) end) end) end function SWEP:CanSecondaryAttack()     if(regenerations <= 0) then         self.Owner:ChatPrint("You're out of regenerations.")         return false     end end
assuming you know how to do this (because I don't) you would just do a counter statement for ENT:Use(a,c). On the Wiki: ENTITY/Use There's a USE Enumerations page. Where it looks like you can just set its state to off.
if self:SecondaryAttack() then regenerations = regenerations -1 Would that work?
Doesn't work
No because...well I don't really know why but it wouldn't. How about instead of regenerations = -1 you do regenerations = regenerations - 1
Can I just say that you are storing your counter in a global variable which will be the same for every player on the server.
Will a local variable save it for only the player that uses it?
No. you need to store the data on the weapon object
So in the weapon function? So SWEP:SecondaryAttack()
No, to store a variable on the weapon object you define it as part of the SWEP, e.g: SWEP.Regenerations = 3 This goes outside any of the functions. Then, in SWEP:SecondaryAttack() at the bottom: self.Regenerations = self.Regenerations - 1 Also, in SWEP:CanSecondaryAttack() you need to return true if they are permitted to use the secondary attack: function SWEP:CanSecondaryAttack() if (self.Regenerations >= 0) then return true else self.Owner:ChatPrint("You're out of regenerations.") return false end end (I would have put that into a code block, but for the life of me cannot get that to not format extremely obscurely. Not sure what is going on there so if someone could let me know, thanks!)
Doesn't work.
What do you mean? Are you getting an error?
Yes [ERROR] addons/doctorwhorp_regeneration_swep_1486986506/lua/weapons/regeneration_admin.lua:105: attempt to perform arithmetic on global 'regenerations' (a nil value)   1. unknown - addons/doctorwhorp_regeneration_swep_1486986506/lua/weapons/regeneration_admin.lua:105
Can you copy and paste your code as it is currently?
SWEP.Regenerations = 3 function SWEP:SecondaryAttack() -- Fast Regeneration     regenerated = false     if self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") != nil then         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_Head1") )         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_L_Foot") )         ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") )         end     if SERVER then         self.Owner:Freeze(true)         self.Owner:GodEnable()     end     for k, v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound02) end     timer.Simple(1, function()     local model = table.Random(regen.models)     self.Owner:SetModel( model )     self.Owner:SetModelName( model )     timer.Simple(1, function()     self.Owner:Freeze(false)     self.Owner:SetHealth(regen.FastRegenHealth)     self.Owner:StopParticles()     lastregen = regen.models     regenerated = true     self.Regenerations = self.Regenerations -1     timer.Simple(5, function()     if SERVER then         self.Owner:GodDisable()     end     timer.Create("regencyclefast",600,4,function()         if regenerated == true then             for k,v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound03) end             self.Owner:SetHealth(self.Owner:Health()+500)         end     end) end) end) end) end function SWEP:CanSecondaryAttack()     if (self.Regenerations >= 0) then         return true     else         self.Owner:ChatPrint("You're out of regenerations.")         return false     end end
local regens = 3 function SWEP:SecondaryAttack()          regens = regens - 1     if regens <= 0 then return nil end          self:ThrowChair( "models/props_c17/FurnitureChair001a.mdl" ) end so for yours it would be local Regenerations = 3 function SWEP:SecondaryAttack() -- Fast Regeneration regenerated = false if self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") != nil then ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_Head1") ) ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_L_Foot") ) ParticleEffectAttach( "generic_smoke", 4, self.Owner, self.Owner:LookupBone("ValveBiped.Bip01_R_Foot") ) end if SERVER then self.Owner:Freeze(true) self.Owner:GodEnable() end for k, v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound02) end timer.Simple(1, function() local model = table.Random(regen.models) self.Owner:SetModel( model ) self.Owner:SetModelName( model ) timer.Simple(1, function() self.Owner:Freeze(false) self.Owner:SetHealth(regen.FastRegenHealth) self.Owner:StopParticles() lastregen = regen.models regenerated = true Regenerations = Regenerations - 1 if Regenerations <= 0 then self.Owner:ChatPrint("You're out of Regenerations") return nil end timer.Simple(5, function() if SERVER then self.Owner:GodDisable() end timer.Create("regencyclefast",600,4,function() if regenerated == true then for k,v in pairs(ents.FindInSphere(self.Owner:GetPos(),128)) do v:EmitSound(sound03) end self.Owner:SetHealth(self.Owner:Health()+500) end end) end) end) end) end Yes I'm aware it's not the way to do it correctly but it should work, just not the way you want it to.
I managed to get it to work. By editing your method. if Regenerations <= 0 then self.Owner:ChatPrint("You're out of regenerations.") self:SetNextSecondaryFire(CurTime() + 1200) end
but dude, people just explained you that Regenerations will be equal for ALL weapons than just the one you own, change it to self.Regenerations and be sure to add SWEP.Regenerations above
This^ Using anything besides SWEP.Regenerations and self.Regenerations will cause many, many issues. Please don't do that.
I say live and let learn. Best way to get good I think
Already did.
But uh, does it work then? If you have a global variable it won't work as intended.
Why not consume secondary ammo?
It does work. And I do not have a global variable for it. I used a weapon object variable as someone stated above ^ It's SWEP.Regenerations = 13 self.Regenerations = self.Regenerations - 1 if self.Regenerations <= 0 then self:SetNextSecondaryFire(CurTime() + 99999999999999999) self:SetNextPrimaryFire(CurTime() + 99999999999999999) end
You should be using the CanSecondaryAttack / CanPrimaryAttack hooks and returning false rather than setting the next fire time to CurTime() + 99999. Using ammo would also make sense, yes, because depending on your HUD it would display how many times you can use it. Then if you don't want the primary fire to work when you are out of secondary ammo, you would check in the SWEP:CanPrimaryFire hook to see if you have any secondary ammo remaining, and if not, return false.
I tried this but it does not work. Is it because it's a weapon object variable? function CanPrimaryAttack()     if self.Regenerations <= 0 then         self.Owner:ChatPrint("You're out of regenerations.")         return false     end end function CanSecondaryAttack()     if self.Regenerations <= 0 then         self.Owner:ChatPrint("You're out of regenerations.")         return false     end end
Really? You just put CanPrimaryAttack() CanPrimaryAttack() is a function of SWEP.
I noticed that. I'm a dumbass.
Sorry, you need to Log In to post a reply to this thread.