• Debouncing
    4 replies, posted
Basically, I've got an entity, and the ENT:Use() code is firing an average of 13 times each time you press E on it, I want it to only fire once. In ROBLOX we used a process called debouncing, where you could throw in a wait(num) function to have it disabled for a time, but I noticed in gmod lua there's no such function, so is there a way I could make a debounce or a way I could stop it from firing 13 times? Code in ENT:Use() : function ENT:Use(activator, caller) if activator:IsPlayer() then player = activator; if nexus.player.CanAfford(player, 5) then nexus.player.GiveCash(player, -5, "Bought a coke", false) nexus.inventory.Update(player, "Coke", 1, true); end end end
[lua] local debounce = false --Define our variable function ENT:Use(activator, caller) if debounce then return end --If debounce is true, don't run. if activator:IsPlayer() then player = activator; if nexus.player.CanAfford(player, 5) then nexus.player.GiveCash(player, -5, "Bought a coke", false) nexus.inventory.Update(player, "Coke", 1, true); debounce = true --Set debounce to true timer.Simple(5,function() debounce=false end) --Let the script run again after 5 seconds end end end [/lua] And use [lua] tags for code, thanks.
You could try to change the usetype.
[QUOTE=skullorz;33245787][lua] local debounce = false --Define our variable function ENT:Use(activator, caller) if debounce then return end --If debounce is true, don't run. if activator:IsPlayer() then player = activator; if nexus.player.CanAfford(player, 5) then nexus.player.GiveCash(player, -5, "Bought a coke", false) nexus.inventory.Update(player, "Coke", 1, true); debounce = true --Set debounce to true timer.Simple(5,function() debounce=false end) --Let the script run again after 5 seconds end end end [/lua] And use [lua] tags for code, thanks.[/QUOTE] Thanks! And I tried using lua tags, but it didn't really work that well, I probably did it wrong.
[QUOTE=Wizard of Ass;33245797]You could try to change the usetype.[/QUOTE] This. [b][url=http://wiki.garrysmod.com/?title=Entity.SetUseType]Entity.SetUseType [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] Setting a variable to true and false after 5 seconds sounds very limiting.
Sorry, you need to Log In to post a reply to this thread.