Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a function execute once and only once?

I only want a certain function to execute once, and then the next time the same event happens, another function will follow.

Here's my current code for the first response.

(I'm making a text-based adventure)

local gui = script.Parent.Parent --The script is inside a TextBox which is inside of a ScreenGUI
local frame = gui
local text1 = frame.TextLabel
local response = gui.Response

local boy = "this way, child"
local girl = "this way, child"
local other = "don't know? follow me, 'it'"


function choice1()
if response.Text == "boy" then
		for i = 1,string.len(boy),1 do
		sound:play()
		text1.Text = string.sub(boy,1,i)
		wait(0.1)
		end
	else if response.Text =="girl" then
		for i = 1,string.len(girl),1 do
		sound:play()
		text1.Text = string.sub(girl,1,i)
		wait(0.1)
		end
	else if response.Text == nil or "" then
		for i = 1,string.len(other),1 do
		sound:play()
		text1.Text = string.sub(other,1,i)
		wait(0.1)
		end
return true
	end
	
		end
end
end

response.FocusLost:connect(function()
	if 1==1 then	
		choice1()
	end
	
	for i = 0,1,0.1 do
		response.Transparency = i
		text1.TextTransparency = i
		wait(0.1)
	end
	text1.Text = ""
	response.Text = "[ENTER A RESPONSE]"
end)

1 answer

✓ Accepted answer

One way you can do this is to set a variable to true once the function has executed.

local FuncExecuted = false

function f1()
	if not FuncExecuted then
		print("Hai")
		FuncExecuted = true
	end
end

function f2()
	f1()
	f1()
	f1()
end

f2()

This discussion is preserved as it was. New answers, comments, and votes are closed.