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
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)
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.
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