Add a delay to a function after clicking a TextButton

I wanna make the object explode 15 seconds after a TextButton has been clicked. The problem with the script right now is that the explosion happens 15 seconds after you join, rather than after the TextButton has been clicked. Is there any way to fix this? Thank you.

local statue = game.Workspace:WaitForChild("EggStatue")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function explode ()
	local explosion = Instance.new('Explosion')
	explosion.Position = statue.Position
	explosion.Parent = game.Workspace
	explosion.BlastRadius = 30
	explosion.DestroyJointRadiusPercent = 0
end

script.Parent.MouseButton1Click:Connect(delay(15, explode)) -- The part that most likely matters the most

The issue here is that delay runs immediately even if the button isnt clicked.

after 15 seconds, itll run the function explode, you can go around this by calling explode with connect and adding task.wait(15) at the top of the function.

2 Likes

It’s because you need to pass a function into the Connection, you’re calling it.

If you wanna delay, I would say to do it with a debounce system

local statue = game.Workspace:WaitForChild("EggStatue")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local debounce = false

local function explode ()
	local explosion = Instance.new('Explosion')
	explosion.Position = statue.Position
	explosion.Parent = game.Workspace
	explosion.BlastRadius = 30
	explosion.DestroyJointRadiusPercent = 0
end

script.Parent.MouseButton1Click:Connect(function()
	if debounce then
		return
	end
	debounce = true
	task.wait(15)
	explode()
	debounce = false
end) 

So that way no one can spam it whilst the 15 seconds aren’t up, making it be able to be exploded once every 15 seconds

3 Likes