• SWEP Functions Running Multiple Times
    6 replies, posted
Essentially I have a function: function SWEP:SecondaryAttack() local ply = self:GetOwner() if(mode=="explosion") then mode = "contagion" elseif (mode == "contagion") then mode = "explosion" else mode = "explosion" end ply:ChatPrint("TRAP MODE: " .. mode) end And whenever I right click with the swep it gives me TRAP MODE: contagion TRAP MODE: explosion TRAP MODE: contagion TRAP MODE: explosion TRAP MODE: explosion TRAP MODE: contagion TRAP MODE: contagion TRAP MODE: explosion TRAP MODE: contagion TRAP MODE: explosion TRAP MODE: explosion TRAP MODE: contagion TRAP MODE: contagion In groups of four The same happens in the primary fire function: function SWEP:PrimaryAttack() local ply = self:GetOwner() local target = ply:GetEyeTrace().Entity print(target) if(IsEntity(target) and target:GetClass() == "sent_airdropcrate") then ply:ChatPrint("Locking onto crate...") timer.Simple( 10, function() armCrate(mode,ply) end) else ply:ChatPrint("ERROR: No crate found!") end end function armCrate( trapType, ply ) ply:ChatPrint("CRATE ARMED") end Where it prints Entity [330][sent_airdropcrate] Locking onto crate... Entity [330][sent_airdropcrate] Locking onto crate... Entity [330][sent_airdropcrate] Locking onto crate... Locking onto crate... (10 seconds later) CRATE ARMED CRATE ARMED CRATE ARMED CRATE ARMED I'd like all of these things to happen only once if possible. Anyone have any ideas?
Awesome! Now we're down to doubles instead of quadruplets Entity [0][worldspawn] ERROR: No crate found! ERROR: No crate found! TRAP MODE: explosion TRAP MODE: explosion Entity [0][worldspawn] ERROR: No crate found! ERROR: No crate found! TRAP MODE: contagion TRAP MODE: contagion Entity [0][worldspawn] ERROR: No crate found! ERROR: No crate found! TRAP MODE: explosion TRAP MODE: explosion
[b]DONT DO THIS![/b] Do not just chuck if not IsFirstTimePredicted() then return end At the top of your SWEP functions, it will break prediction. You know how you get real pissed off because you shoot a guy and there’s definitely blood spatter on the wall but the guy takes no damage? That’s because of this. The only thing that should be surrounded by that IsFirstTimePredicted check is the timer.
Perfect! To sum it up it seems like we were calling 4 times because it ran on client and server and there was a prediction for both. 1 * 2 * 2 = 4
You should also use a self variable instead of a local for the mode. Otherwise, all weapon instances will change modes when a single one secondary fires.
Sorry, you need to Log In to post a reply to this thread.