Add cooldown on player touching part?

I’d like to make it so that a GUI pops up once when a player touches a part and has a 10 second cooldown before they can touch that part again, but whenever the player touches the part it pops up 30+ GUIs a second and has no cooldown. I’ve tried adding a wait(10) or a debounce but neither of those really worked.

Code:

local Players = game:GetService("Players")

local function onTouched(hit)
	local character = hit.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")

	if humanoid then
		print("Player touched AreaTransitionMainGym!")
		local new = game.ReplicatedStorage.MainGymPopup:Clone()
		new.Parent = script.Parent.PopupGuiGym
	end
end

local areaTransitionMainGym = game.Workspace:WaitForChild("AreaTransitionMainGym")
areaTransitionMainGym.Touched:Connect(onTouched)
3 Likes
local Cooldown = false

-- function

Cooldown = true

wait(10)

Cooldown = false
4 Likes

I’m actually so out of it, thank you.

2 Likes
local Players = game:GetService("Players")
local Cooldown = false

local function onTouched(hit)
	local character = hit.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")

	if humanoid and not Cooldown then
		local new = game.ReplicatedStorage.MainGymPopup:Clone()
		new.Parent = script.Parent.PopupGuiGym

		Cooldown = true
		wait(10)
		Cooldown = false
	end
end

local areaTransitionMainGym = game.Workspace:WaitForChild("AreaTransitionMainGym")
areaTransitionMainGym.Touched:Connect(onTouched)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.