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

It would, but you most likely wont see it happen due to all the waits.

Would I put the return after every wait to get the effect I’m going for?

return only works once in functions as it ends the function, if you would want the code to yield, try using coroutines

What in the world are you trying to?

Looks like he is trying to make a bomb

Do you mean yielding the function from running like what @DasKairo said?

I think task.spawn() or coroutines may help.

They basically creates a thread in the script, so the code in there will only run in that thread and loops can be placed in there without interrupting the rest of the code.

This is a simple way of creating a thread with task.spawn() and yielding it later

local thread = task.spawn(function()
print("1")
task.wait(2)
print("2")
task.wait(2)
print("3")
end)
task.wait(3)
task.cancel(thread)

I didn’t test it yet, and I’m not sure if it really cancels, but I think it does.

coroutines is the same task.spawn(), but with more features for complex scripting

To elaborate, this is a hot potato game. The code I put flashes the potato red and then the flashing speeds up, then it explodes and whoever is holding it blows up. When the potato gets passed to another player, I want this function to go back to the top to reset the explosion timer.

I am aware. But I need more information from OP about the topic. The replies so far, have been very ineffecient. (Not gonna judge, just putting out a fact here)

1 Like

Could I do something like this?

game.ReplicatedStorage.HotPotatoEvents.TimerReset.Event:Connect(function()--BindableEvent is from a seperate script that handles the model being swapped from player to player
	task.cancel(ExplosionFunction)
	wait()
	task.spawn(ExplosionFunction)
end)
1 Like

Just an idea

task.spawn(function()

local Time = 2

while Time >= .1 do
 -- Part 1 code
task.wait(Time/2)
-- Part 2 code
task.wait(Time/2)
Time -=.1
end


-- Explode

end)

Obviously this isnt great but, Just an idea neither less.

Yes, but I have a feeling that it may error because you’re canceling what’s already canceled.

I may be wrong, but just in case, maybe add a variable and a sanity check to make sure that the thread is resuming to cancel it. Run the game and then tell us if it worked.

local threadResuming = false

game.ReplicatedStorage.HotPotatoEvents.TimerReset.Event:Connect(function()--BindableEvent is from a seperate script that handles the model being swapped from player to player
	if threadResuming then
		task.cancel(ExplosionFunction); threadResuming = false
	end
	task.wait()
	task.spawn(ExplosionFunction); threadResuming = true
end)

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.