How do you make an anonymous function run only once?

I want to make this function run only once and then never again:

button.MouseButton1Click:Connect(function()
		InfoLabel.Text = "You Generated a New Color "..clicks + 1 .." Time(s)! Keep Going."
		PreviewLoopKeeper = false

The issue is, even after the while loop it’s in breaks, it still runs.

I’ve tried solutions like disconnecting it, but it didn’t work. Maybe I did it wrong.

Here is the whole while loop:

-- Preview
while PreviewLoopKeeper do
	RGB_RNG.RGBrandomizer(InfoLabel)
	RGB_RNG.RGBrandomizer(frame)

	button.MouseButton1Click:Connect(function()
		InfoLabel.Text = "You Generated a New Color "..clicks + 1 .." Time(s)! Keep Going."
		PreviewLoopKeeper = false
	end)
	task.wait(1)
end

Whole script:

-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RGB_RNG = require(ReplicatedStorage.MODULES.RGBrandomizer)
local button = script.Parent.Parent.ChangeColor
local InfoLabel = script.Parent.Parent.TextLabels.InfoLabel
local clicks = 0
local frame = script.Parent.Parent.ColorChangingFrame
local PreviewLoopKeeper = true
local debounce = false
----------------------------------------------------------------------------------------------------

-- Preview
while PreviewLoopKeeper do
	RGB_RNG.RGBrandomizer(InfoLabel)
	RGB_RNG.RGBrandomizer(frame)

	button.MouseButton1Click:Connect(function()
		InfoLabel.Text = "You Generated a New Color "..clicks + 1 .." Time(s)! Keep Going."
		PreviewLoopKeeper = false
	end)
	task.wait(1)
end
----------------------------------------------------------------------------------------------------

-- Color Generated Amount Counter
button.MouseButton1Click:Connect(function()
	PreviewLoopKeeper = false
	if not debounce then
		debounce = true

		clicks += 1
		RGB_RNG.RGBrandomizer(InfoLabel)
		InfoLabel.Text = "You Generated a New Color "..clicks + 1 .." Time(s)! Keep Going."
		InfoLabel.TextSize = 18

		task.wait(.5)
		debounce = false
	end
end)
----------------------------------------------------------------------------------------------------

1 Like

change it to

button.MouseButton1Click:Once(function()

and if you want it to wait until they press it you can do

button.MouseButton1Click:Wait()
3 Likes
-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RGB_RNG = require(ReplicatedStorage.MODULES.RGBrandomizer)
local button = script.Parent.Parent.ChangeColor
local InfoLabel = script.Parent.Parent.TextLabels.InfoLabel
local clicks = 0
local frame = script.Parent.Parent.ColorChangingFrame
local PreviewLoopKeeper = true
local debounce = false
----------------------------------------------------------------------------------------------------

-- Preview
while PreviewLoopKeeper do
	RGB_RNG.RGBrandomizer(InfoLabel)
	RGB_RNG.RGBrandomizer(frame)

	button.MouseButton1Click:Once(function() -- Once Instead Connect
		InfoLabel.Text = "You Generated a New Color ".. tostring(clicks + 1) .." Time(s)! Keep Going."
		PreviewLoopKeeper = false
	end)
	
	task.wait(1)
end
----------------------------------------------------------------------------------------------------

-- Color Generated Amount Counter
button.MouseButton1Click:Connect(function()
	PreviewLoopKeeper = false
	if not debounce then
		debounce = true

		clicks += 1
		RGB_RNG.RGBrandomizer(InfoLabel)
		InfoLabel.Text = "You Generated a New Color " .. tostring(clicks + 1) .." Time(s)! Keep Going."
		InfoLabel.TextSize = 18

		task.wait(.5)
		debounce = false
	end
end)
----------------------------------------------------------------------------------------------------
1 Like

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