Is there a way to make a function stop in the middle of running and go back to the top?

I get multiple errors:
“Attempt to connect failed: Passed value is not a function”
“attempt to call a thread value”
“attempt to call an Instance value”

Uhm okay. What you could do, is implement a loop of some sort. Uncertain what script.Parent.Parent is, but let’s say it’s your potato. I assume that the potato is almost always in use, so trying not to use a loop here is not relevant. I also assume that all new potatoes will have a new copy of the code below.

local Potato = script.Parent.Parent
local SavedCurrentPlayer
local CurrentPlayer
local MaxTimer = 15
local CurrentTimer = MaxTimer

Potato.AncestryChanged:Connect(function()
	CurrentPlayer = NewPlayer -- Define how you get newplayer yourself
	if not SavedCurrentPlayer then -- First time player will be set to SavedCurrentPlayer
		SavedCurrentPlayer = CurrentPlayer
	end
end

repeat
	task.wait(1)
-- If SavedCurrentPlayer is yet to be set, or if SavedCurrentPlayer is nil/left the game, reset timer all the time.
-- It will also never go to the "or" statement, before CurrentPlayer and SavedCurrentPlayer
-- is set to something, since SavedCurrentPlayer is set lastly in the code above
	if not SavedCurrentPlayer or CurrentPlayer ~= SavedCurrentPlayer then
		CurrentTimer = MaxTimer
	end
	CurrentTimer -= 1
until CurrentTimer == 0
-- Explode Code here below!
-- Do something to SavedCurrentPlayer

I should’ve probably posted this before, but this is the other code that creates and gets the potato around:


local RS = game:GetService("ReplicatedStorage")
local Event = RS.HotPotatoEvents.HotPotatoSelected
local Start = RS.HotPotatoEvents.PotatoCreateEvent

local Potato = Instance.new("Accessory")
		RS.Handle.Parent = Potato
		local Attatchment = Instance.new("Attachment")
		Attatchment.Name = "BodyFrontAttachment"
		Attatchment.Parent = Potato.Handle
Potato.Parent = RS
Potato.Name = "Potato"

Attatchment.Orientation = Vector3.new(0,0,-90)
	print("Potato Instance has been created")

Event.Event:Connect(function()
	local Players = game.Teams.Player:GetPlayers()
	local random = Players[math.random(1,#Players)]
print("Event Happened")
	Start.Event:Connect(function()
		
		local PotatoClone = Potato:Clone()

		PotatoClone.Parent = random.Character
		print("Potato Instance has been attached to the player")

		local cooldown = true
		
			PotatoClone.Handle.Touched:Connect(function(hit)
			if hit.Name == "HumanoidRootPart" then
				if cooldown then
					cooldown = false
					PotatoClone.Parent = hit.Parent
					game.ReplicatedStorage.HotPotatoEvents.TimerReset:Fire()
					print("Swapped Potato to",hit.Parent)
					wait(2)
					warn("cooldown done")
					cooldown = true
				end

			end
			
			end)
	end)
end)

Start.Event:Connect(function()
	local Players = game.Teams.Player:GetPlayers()
	local random = Players[math.random(1,#Players)]
	local PotatoClone = Potato:Clone()

	PotatoClone.Parent = random.Character
	print("Potato Instance has been attached to the player")

	local cooldown = true

	PotatoClone.Handle.Touched:Connect(function(hit)
		if hit.Name == "HumanoidRootPart" then
			if cooldown then
				cooldown = false
				PotatoClone.Parent = hit.Parent
				print("Swapped Potato to",hit.Parent)

				wait(2)
				warn("cooldown done")
				cooldown = true
			end

		end

	end)
end)

This code is activated out of a voting system. If the Hot Potato mode gets to most votes, then it will call the Events in this script to start everything. I’m not worried currently about ending the round, I just need the timer reset working.

I would use this format:

local function myFunction()
    while true do
    
    print("Do something!")

    if shouldRestart then
        continue
    end    

    break

    end
end

If it gets to the end, it will break and end the function. If shouldRestart is true, it will go back to the top.

Change it how you would like if it isn’t exactly what you are looking for.

Well, if you create a clone of the potato with the script inside, then it should do what you like it to.

It doesn’t destroy it and make a copy for the next player, it passes the same model
with the exact same script around

I found a very strange method, but a method that somehow works:
Sorry for wasting your time

game.ReplicatedStorage.HotPotatoEvents.TimerReset.Event:Connect(function()--BindableEvent is from a seperate script that handles the model being swapped from player to player
	local Clone = script:Clone()
	Clone.Name = "BlowUpPotato"
	Clone.Parent = script.Parent
	script:Destroy()
end)

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