How can i make a debounce for this script?
local UserInputService = game:GetService("UserInputService") local Player = game.Players.LocalPlayer local Character = workspace:WaitForChild(Player.Name) local Humanoid = Character:WaitForChild("Humanoid") local CombatEnabled = false local CombatAnim = script:WaitForChild("CombatAnim") local animTable = {"rbxassetid://5021409784";"rbxassetid://5021418974";"rbxassetid://5021422938";"rbxassetid://5021425832"} UserInputService.InputBegan:Connect(function(Input,IsTyping) if not IsTyping then if Input.KeyCode == Enum.KeyCode.E then CombatEnabled = true print("Combat Enabled!") spawn(function() while CombatEnabled == true do local randomAnimation = math.random(1, #animTable) local randomAnim_Value = animTable[randomAnimation] CombatAnim.AnimationId = tostring(randomAnim_Value) local CombatAnimLoader = Humanoid:LoadAnimation(CombatAnim) -- game.ReplicatedStorage.Combat:FireServer("CombatEnabled",animTable) CombatAnimLoader:Play() print(randomAnim_Value) wait(CombatAnimLoader.Length + 0.2) if CombatEnabled == false then print("Combat Disabled!") break end end end) end end end) UserInputService.InputEnded:Connect(function(Input, IsTyping) if not IsTyping then if Input.KeyCode == Enum.KeyCode.E then CombatEnabled = false end end end)
Hello.
You must make a debounce by adding a debounce as a variable.
Next, add an if statement checking if debounce == false
.
If it is, then the debounce will be equal to true.
Finally, you add a wait() (I used 1.5 seconds) and then set the debounce back to false.
Code:
local UserInputService = game:GetService("UserInputService") local Player = game.Players.LocalPlayer local Character = workspace:WaitForChild(Player.Name) local Humanoid = Character:WaitForChild("Humanoid") local CombatEnabled = false local CombatAnim = script:WaitForChild("CombatAnim") local animTable = {"rbxassetid://5021409784";"rbxassetid://5021418974";"rbxassetid://5021422938";"rbxassetid://5021425832"} local debounce = false UserInputService.InputBegan:Connect(function(Input,IsTyping) if not IsTyping then if Input.KeyCode == Enum.KeyCode.E and not debounce then debounce = true CombatEnabled = true print("Combat Enabled!") spawn(function() while CombatEnabled == true do local randomAnimation = math.random(1, #animTable) local randomAnim_Value = animTable[randomAnimation] CombatAnim.AnimationId = tostring(randomAnim_Value) local CombatAnimLoader = Humanoid:LoadAnimation(CombatAnim) -- game.ReplicatedStorage.Combat:FireServer("CombatEnabled",animTable) CombatAnimLoader:Play() print(randomAnim_Value) wait(CombatAnimLoader.Length + 0.2) if CombatEnabled == false then print("Combat Disabled!") break end end end) wait(1.5) debounce = false end end end) UserInputService.InputEnded:Connect(function(Input, IsTyping) if not IsTyping then if Input.KeyCode == Enum.KeyCode.E then CombatEnabled = false end end end)