Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
ElBamino 153
2 years ago
Edited 2 years 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
        humanoid:LoadAnimation(animation):Play()
        wait(10)
        local animationTracks = humanoid:GetPlayingAnimationTracks()
        for _, t in pairs (animationTracks) do
            t:Stop()
            player.leaderstats.Tix.Value = player.leaderstats.Tix.Value + 100
            debounce = false
        end
    end
end

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

2 answers

Log in to vote
1
Answered by
Cirillix 110
2 years ago

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
        humanoid:LoadAnimation(animation):Play()
        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
        plr.leaderstats.Tix.Value = plr.leaderstats.Tix.Value + 100
    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 153 — 2y
0
You did a really good job explaining so, I appreciate that. I understand a lot better now. ElBamino 153 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
local debounce GetChildren ("debounce")
 if debounce.Working = false then
changeValue.debounce.Working = true

if value being subtracted instead of being added then
 Stop
0
Uhm, I don't really know how to respond to this without being rude. I appreciate you trying to help though. ElBamino 153 — 2y

Answer this question