How To Restart A Function?

Im not good a scripting but Ive been trying how to figure out how to reset a function!

So like if _G.Number isnt 3 then it would keep reseting until it is 3
Im not going to use while true do

spawn(function(Number)
		if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			--reset
		end
end)

I don’t really understand what the hell you are trying to do but this could help:

function checkIfThree(Number)
      if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			checkIfThree()
		end
end

Not sure, but it will probably cause a stack overflow

You could also copy paste the function xinfinite times and it would probably work.



spawn(function(Number)
		if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			if _G.NumberVar == 3 then
		print("Number 3")
		else
			print("Not Number 3")
			--reset
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
		end
end)

It looks like you’re trying to poll the function to detect when the value is what you want. Don’t do that.

What you can do is either use BindeableEvents or a custom wrapper like

local Value: number = 1

local function OnValueChange(): ()
	if Value == 3 then
		...
	end
end

local function ChangeValue(NewValue: number): ()
	Value = NewValue
	OnValueChange()
end

ChangeValue(2)

Also don’t use _G, it opens up race conditions and doesn’t get typed. ModuleScripts are the way to go with sharing variables.

hi i would like to also suggest a solution which is with a loop

repeat
    print("Number is not equal 3")
    task.wait()
until _G.Number == 3

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