Debounce doesn't work for runservice

So, I’m trying to make a door open when a player is in 25 studs within it, this is what it does, no errors btw:

help - YouTube
This is my script:

local RP = script.Parent.PrimaryPart
local DetectionPart = script.Parent:WaitForChild("Part")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local debounce = false

game:GetService("RunService").Heartbeat:Connect(function(dt)
        for i,plr in pairs(Players:GetPlayers()) do
        if not debounce then
            local Radius = 25
            local Character = plr.Character 
			if not Character then continue end
            local door = {}
            local HRP = Character.HumanoidRootPart
            if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
                local DoorTween = TweenService:Create(RP, TweenInfo.new(0.25,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(-180),0)})
                DoorTween:Play()
                wait(1)
                debounce = true
               
            elseif (HRP.Position - DetectionPart.Position).Magnitude > Radius and debounce then
                local EndDoorTween = TweenService:Create(RP, TweenInfo.new(0.5,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(180),0)})
                EndDoorTween:Play()
                wait(1)
                debounce = false
            end
        end
    end
end)

Is this is a good way to make a cooldown? - Help and Feedback / Scripting Support - DevForum | Roblox check here some one already did this

1 Like