How to make function call itself again under certain circumstance?

I know the title seems confusing, so I’ll try to explain this as best as I can.
There’s a part in my game where you have an ice cream sandwich eating contest with a bot, and if the bot finishes its ice cream sandwiches before you, they reset, and it starts over. It will keep resetting until the player wins.

I’ve got everything worked out but the function starting over again. You can eat, the bot works, you can win and lose, they reset if you lose, but I can’t get the function to run again.

I’ve tried looking on the developer hub and recalling the function inside itself. Neither worked.

-- wait(5)

local function IceCreamBattle(playerIceCreams, botIceCreams)
local PlayerWon
script.Parent.Enabled = true

script.Parent.EatButton.MouseButton1Click:Connect(function()
	local chance = math.random(1,3)
	if chance == 1 then
		local IceCreams = workspace.IceCreams:GetChildren()
		for IceCream = 1, #IceCreams, 1 do
			local randomIceCream = IceCreams[math.random(1, 1)]
			randomIceCream:Destroy()
		end
		if #IceCreams == 0 then
			script.Parent.Enabled = false
			PlayerWon = true
		end
	end
end)

while true do
local BotIceCreams = workspace.BotIceCreams:GetChildren()
local randomBotIceCream = BotIceCreams[math.random(1, 1)]
local randomWait = math.random(0.5, 1)
wait(randomWait)
	if #BotIceCreams == 0 and PlayerWon == nil then
		PlayerWon = false
		script.Parent.Enabled = false
		break
	end
	if PlayerWon == true then break end
randomBotIceCream:Destroy()
end

	if PlayerWon == false then
	wait(1)
	local NewPlayerIceCreams = game.ReplicatedStorage:WaitForChild("NewPlayerIceCreams", 10):Clone()
	local NewBotIceCreams = game.ReplicatedStorage:WaitForChild("NewBotIceCreams", 10):Clone()
	NewPlayerIceCreams.Parent = workspace
	NewBotIceCreams.Parent = workspace
	NewBotIceCreams = botIceCreams
	NewPlayerIceCreams = playerIceCreams
	wait(5)
	IceCreamBattle(NewPlayerIceCreams, NewBotIceCreams)
end
end

IceCreamBattle(workspace.IceCreams, workspace.BotIceCreams)

Please help!

1 Like

Firstly, please use task.wait() as it’s more efficient and the new version of wait.

Secondly, you are calling the function BELOW the while loop.

If you didn’t know already, a loop will cause the code to literally loop meaning anything below it simply wont run. Nothing runs below a while loop, (unless the condition of the loop ever changes and causes it to stop looping).

Simply, call the function above the loop if you can. Or in another script if necessary.

I changed all of the wait()'s into task.wait()'s.

But the while loop is broken despite PlayerWon’s value.
And plus, the lines below the loop run. It duplicates the ice cream sandwiches then puts them into the workspace (which is instructed after the while loop). it’s the function that doesn’t run which is the problem.

May just be because you copy and pasted your code from studio but you forgot to add an “end” to the function. This may be the problem.

I’ve found the solution.

Long story short, I forgot to remove the old empty folder, so the script was getting confused on what the new ice cream sandwiches were, and not working properly.

Thanks for the assistance, Amritss.

The function has an end. It’s at the bottom of my script. Also none of that was copy pasted, lol.