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!