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

My debounce is looping instead of having an actual debounce?

Asked by
L30N_C 27
2 years ago

ok so, i am making a magnitude and when you are in that area a tween will play. Since this script is a loop i have to make a debounce so it only tweens once, but my debounce doesnt work so it still loops.

Can anyone see what is wrong with my debounce?

btw, this is not the whole script, just the part with the debounce!

local CanActive = true

local Mag = (v.Position - HumanoidRootPart.Position).magnitude


if Mag <= 6 and CanActivate then

    CanActivate = false

    EClip:TweenSize(UDim2.new(0, 120,0, 53),"Out","Bounce", .5, true)   

    print("Player is near Clothes")
else
    CanActivate = true
    print("Player is no longer near Clothes")

end
0
Can you post the entire code, I cant find the error from this part JustinWe12 723 — 2y
0
there is no error, the code just loops over and over, this code is inside a run.renderstepped function L30N_C 27 — 2y
0
I posted more from the script L30N_C 27 — 2y

3 answers

Log in to vote
0
Answered by
L30N_C 27
2 years ago

Here is some more of the script, not all of it. Only the important parts

local RunScervice = game:GetService("RunService")

local eParts = game.Workspace.EClothesFolder:GetChildren()

local CanActivate  = true

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local EClip = script.Parent.Parent

function RenderStepped()

    for i , v in pairs(eParts) do
        local CanActive = true

        local Mag = (v.Position - HumanoidRootPart.Position).magnitude


        if Mag <= 6 and CanActivate then

            CanActivate = false

            EClip:TweenSize(UDim2.new(0, 120,0, 53),"Out","Bounce", .5, true)   

            print("Player is near Clothes")
        else
            CanActivate = true
            print("Player is no longer near Clothes")

        end
    end



end






RunScervice.RenderStepped:Connect(RenderStepped)
Ad
Log in to vote
0
Answered by 2 years ago

I think the debounce isn't working because you changed debounce to false and in the next frame, you turn it back to true. When Mag<=6 and Canactivate is true, then Canactivate is equal to false. When the if statement is run again the next frame, the if statement will be false and the else will fire causing Canactivate to become true. Basically, in the first frame Canactivate is true, in the next frame it is false and in the third frame it is true again. Because Canactivate is false for a very short period of time, it seems like there is no debounce.

0
i know but how do i fix it? L30N_C 27 — 2y
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

The CanActivate variable is created inside the RenderStepped, meaning no value is kept for the next RenderStepped event, place it on the outside of the renderstepped event and if you need to have a debounce for each part, use a table (example below)

local Debounce ={}

-- iterating parts
for i,v in ipairs(partsTable) do
    if not Debounce[v] then
        print(v)
        Debounce[v] = true

        -- prevents running again till "Debounce[v] = nil"
    end
end

Answer this question