Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I want my function to restart instead of resuming using coroutine.resume(StartGame)?

code:

local StartGame = coroutine.wrap(function()
	ChooseGame()
	print(rm)
	game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
	for i,v in pairs(game.Players:GetChildren()) do
		v.Backpack.inGame.Value = "Game"
	end
	game.ReplicatedStorage.GameStatus.Value = "Game"
	if rm == 1 then
		game.ReplicatedStorage.GameStatus.Value = "Game"
		local WaterRunMap = game.ServerStorage.WaterRun:Clone()
		WaterRunMap.Parent = game.Workspace
		game:GetService("ReplicatedStorage").GameTime60:Fire()
		game:GetService("ReplicatedStorage").WaterRun:FireAllClients()
		wait(2.5)
		for i,v in pairs(game.Players:GetChildren()) do
			v.Character.HumanoidRootPart.CFrame = CFrame.new(-207.5, 17.5, 151.5)
		end
		for t = 60,0,-1 do
			game.ReplicatedStorage.GameTime.Value = t
			wait(1)
		end
		EndGame()
		Intermission()
	elseif rm == 2 then
		game.ReplicatedStorage.GameStatus.Value = "Game"
		local BombBombRainMap = game.ServerStorage.BombBombRain:Clone()
		BombBombRainMap.Parent = game.Workspace
		game.ServerScriptService.BombRain.Disabled = false
		game:GetService("ReplicatedStorage").GameTime60:Fire()
		game:GetService("ReplicatedStorage").BombBombRain:FireAllClients()
		wait(2.5)
		for i,v in pairs(game.Players:GetChildren()) do
		v.Character.HumanoidRootPart.CFrame = CFrame.new(-43, 2.5, 27)
		end
		for t = 60,0,-1 do
			game.ReplicatedStorage.GameTime.Value = t
			wait(1)
		end
		game.ServerScriptService.BombRain.Disabled = true
		EndGame()
		Intermission()
	elseif rm == 3 then
		game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
		game.ReplicatedStorage.GameStatus.Value = "Game"
		game:GetService("ReplicatedStorage").GameTime120:Fire()
		local SwordFightMap = game.ServerStorage.SwordFight:Clone()
		SwordFightMap.Parent = game.Workspace
		game:GetService("ReplicatedStorage").SwordFight:FireAllClients()
		wait(2.5)
		for i,v in pairs(game.Players:GetChildren()) do
		v.Character.HumanoidRootPart.CFrame = CFrame.new(2, 5, 80)
		end
		for t = 120,0,-1 do
			game.ReplicatedStorage.GameTime.Value = t
			wait(1)
		end
		EndGame()
		Intermission()
	--[[elseif rm == 4 then
		local PlatformRunMap = game.ServerStorage.PlatformRun:Clone()
		PlatformRunMap.Parent = game.Workspace
		if NumPlayers.Value == 2 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 3 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 4 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 5 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 6 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 7 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 8 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 9 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 10 then
			print("PlaceHolder")
		elseif NumPlayers.Value == 11 then -- Sometimes roblox's servers can go 1 player over max So this is for that player 
			print("PlaceHolder")
		elseif NumPlayers.Value == 1 then
			print("Not Enough Players for gamemode!")
			print("Ending game!")
			PlatformRunMap:Destroy()
		end
		--]]
		elseif rm == 4 then --This Map is disabled Because it is a wip
		game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
		game.ReplicatedStorage.GameStatus.Value = "Game"
		local LaserTagMap = game.ServerStorage.LaserTag:Clone()
		LaserTagMap.Parent = game.Workspace
		wait(2.5)
		for i,v in pairs(game.Players:GetChildren()) do
			v.Character.HumanoidRootPart.CFrame = CFrame.new(104, 7.59996033, -35)
		end
		game:GetService("ReplicatedStorage").LaserTag:FireAllClients()
		wait(10)
		for t = 120,0,-1 do
			game.ReplicatedStorage.GameTime.Value = t
			wait(1)
		end
		EndGame()
		Intermission()
	elseif rm == 6 then --This Map is disabled Because it is a wip
		local SewersMap = game.ServerStorage.WIPSewers:Clone()
		SewersMap.Parent = game.Workspace
	end
end)

function Intermission()
	game.ReplicatedStorage.GameStatus.Value = "Intermission"
	for t = 30,0,-1 do
		Intime.Value = t
		wait(1)
	end
	Intime.Value = "Loading Map!"
	wait(1)
	coroutine.resume(StartGame)
end

4 comments

0

show StartGame function

0

So instead of coroutine.resume() starting the code from where it left off is their a function like coroutine.restart()?

0

I have read that their is no restart funcrion how can I work myself around this

2 answers

✓ Accepted answer

A dead coroutine is a dead coroutine, you cannot restart it. You could create a new coroutine or alternatively put the running coroutine into suspended state with the coroutine.yield function, then repeat the necessary code once it is resumed again.

local c = coroutine.create(function(x)
	local n = 0
	while true do
		n = n + x
		print(n)
		x = coroutine.yield()
	end
end)

coroutine.resume(c, 1)
coroutine.resume(c, 2)
coroutine.resume(c, 3)

Before using coroutines however, please consider if you even need them. Most of the time you want the main thread yield until the game loop finishes and rather perform other actions in separate threads if absolutely necessary.

This discussion is preserved as it was. New answers, comments, and votes are closed.