0

# Why is my debounce not working and, why does it add value then subtract that value right after?

ElBamino 104
3 days ago
Edited 3 days ago

Hey scriptinghelpers, two questions same script. So, the script below is a LocalScript in StarterGui, on key pressed (the key is Q/mobile button) run animation. I'm actually having two problems with this script. The first one is my debounce isn't working so, if you press Q repeatedly it just keeps running the animation over and over for each time you press it (meaning it starts over basically). The second problem I'm having is that it adds the value to leaderstats then it immediately subtracts that value it just added, which I don't understand. I appreciate the help. Thanks in advance!

This is a LocalScript in StarterGui

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local cas = game:GetService("ContextActionService")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://27789359"

local debounce = false
function OnKeyPressed(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin and debounce == false then
debounce = true
wait(10)
local animationTracks = humanoid:GetPlayingAnimationTracks()
for _, t in pairs (animationTracks) do
t:Stop()
debounce = false
end
end
end

cas:BindAction("PlayAnim", OnKeyPressed, true, Enum.KeyCode.Q)
cas:SetPosition("PlayAnim", UDim2.new(1, -70, 0, 10))


1

Your debounce isn't working because it's inside a for loop. Also, you cannot change leaderstats values through a local script, you'll have to use a remote event.

Here is a quick way

Local script :

local player = game.Players.LocalPlayer
local character = player.Character

--Add a remote event in the replicatedstorage
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local humanoid = character:WaitForChild("Humanoid")
local cas = game:GetService("ContextActionService")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://27789359"

local debounce = false
function OnKeyPressed(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin and debounce == false then
debounce = true
wait(10)
debounce = false

--I don't know if this is when you want to give the stats
RemoteEvent:FireServer("Tix")

local animationTracks = humanoid:GetPlayingAnimationTracks()
for _, t in pairs (animationTracks) do
t:Stop()
end
end
end

cas:BindAction("PlayAnim", OnKeyPressed, true, Enum.KeyCode.Q)
cas:SetPosition("PlayAnim", UDim2.new(1, -70, 0, 10))



Server script :

local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(plr, stat)
--I recommend you add extra security check there
if stat == "Tix" then
end
end)


But yeah I don't understand what you're trying to do with this

0
Oh sweet, thanks for the help! I appreciate it. I'm not really trying to do much with it, I just was having a hard time because, last time I used RemoteEvent it wasn't working properly so I didn't really understand. I'm only here to learn! The script is just so I can learn about on key pressed and I wanted to add some type of value. Thanks again though. ElBamino 104 — 2d
0
You did a really good job explaining so, I appreciate that. I understand a lot better now. ElBamino 104 — 2d
local debounce GetChildren ("debounce")