Detect when function is completed

hey, i want to fire a function then i want to start it over as soon as the function is completed, to create my own round system. I’ve already tried calling the function inside of the function, however, that only worked after four rounds, after it completely just brakes and it doesn’t work. How can I fix the issue?

Source Code:
(ServerScript)

game.Players.RespawnTime = math.huge
_G["waitTime"] = 4.5
local extraTime = 2.5
local price = 365

game.ReplicatedStorage.BuyMoreTime.OnServerEvent:Connect(function(player)
	if player.leaderstats.Coins.Value >= price then
		player.leaderstats.Coins.Value -= price
		_G.waitTime += extraTime
		game.ReplicatedStorage.DisplayTimer:FireClient(player)
		task.wait(150)
		_G.waitTime -= extraTime
	end
end)

function cleanUp()
	for _, player in pairs(game.Players:GetPlayers()) do
		player:LoadCharacter()
	end
	for i, v in pairs(workspace.Map:GetChildren()) do
		if v:IsA("Part") then
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end
local function round()
	task.wait(3.5)
	local random = math.random(1, 4)
	if random == 1 then
		game.ReplicatedStorage.UpdateRed:FireAllClients()
		game.ReplicatedStorage.UpdatedRed.OnServerEvent:Connect(function(player)
			task.wait(_G.waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= "Red" and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
			task.wait(3)
			cleanUp()
		end)
	elseif random == 2 then
		game.ReplicatedStorage.UpdateYellow:FireAllClients()
		game.ReplicatedStorage.UpdatedYellow.OnServerEvent:Connect(function(player)
			task.wait(_G.waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= "Yellow" and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
			task.wait(3)
			cleanUp()
		end)
	elseif random == 3 then
		game.ReplicatedStorage.UpdateGreen:FireAllClients()
		game.ReplicatedStorage.UpdatedGreen.OnServerEvent:Connect(function(player)
			task.wait(_G.waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= "Green" and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
			task.wait(3)
			cleanUp()
		end)
	elseif random == 4 then
		game.ReplicatedStorage.UpdateBlue:FireAllClients()
		game.ReplicatedStorage.UpdatedBlue.OnServerEvent:Connect(function(player)
			task.wait(_G.waitTime)
			for i, v in pairs(workspace.Map:GetChildren()) do
				if v.Name ~= "Blue" and v:IsA("Part") then
					v.Transparency = 0.7
					v.CanCollide = false
				end
			end
			task.wait(math.sqrt(workspace.Gravity / player.Character.Torso.Position.Y))
			if player.Character.Humanoid.Health ~= 0 then
				if player.MembershipType == Enum.MembershipType.None then
					player.leaderstats.Coins.Value += 50
				elseif player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Coins.Value += 75
				end
			else
				if player.MembershipType == Enum.MembershipType.None then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 50 / 2
					end
				elseif player.MembershipType == Enum.MembershipType.Premium then
					if player.leaderstats.Coins.Value >= 0 then
						player.leaderstats.Coins.Value -= 75 / 5
					end
				end
			end
			task.wait(3)
			cleanUp()
		end)
	end
end
task.wait(3.5)
round()

(LocalScript (to update the text gui))

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(
	1.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)
local player = game.Players.LocalPlayer

if player.MembershipType == Enum.MembershipType.Premium then
	_G.waitTime += 1
end

game.ReplicatedStorage.UpdateRed.OnClientEvent:Connect(function()
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Red"
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(255, 0, 0)
	local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
	t:Play()
	t.Completed:Connect(function()
		player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
	end)
	game.ReplicatedStorage.UpdatedRed:FireServer()
end)
game.ReplicatedStorage.UpdateYellow.OnClientEvent:Connect(function()
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Yellow"
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(255, 255, 0)
	local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
	t:Play()
	t.Completed:Connect(function()
		player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
	end)
	game.ReplicatedStorage.UpdatedYellow:FireServer()
end)
game.ReplicatedStorage.UpdateGreen.OnClientEvent:Connect(function()
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Green"
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(0, 255, 0)
	local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
	t:Play()
	t.Completed:Connect(function()
		player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
	end)
	game.ReplicatedStorage.UpdatedGreen:FireServer()
end)
game.ReplicatedStorage.UpdateBlue.OnClientEvent:Connect(function()
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Text = "Blue"
	player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.TextColor3 = Color3.fromRGB(0, 0, 255)
	local t = ts:Create(player.PlayerGui:WaitForChild("ScreenGui").ReadyUp, ti, {TextTransparency = 1})
	t:Play()
	t.Completed:Connect(function()
		player.PlayerGui:WaitForChild("ScreenGui").ReadyUp.Visible = false
	end)
	game.ReplicatedStorage.UpdatedBlue:FireServer()
end)

Anyone know how I can fix this?

could you put it in a while loop?

nope, the script must have while loop-phobia or something cause everytime i’ve tried that, the game breaks after just one round.

1 Like

ah ok. Sorry I couldn’t help, I just thought that because the round system I’m using uses a while loop.

It’s ok, at least you tried helping me.

1 Like

The only way I think you could do this is with a while loop, but reading the other replies you said it does not work.

Would you mind sharing a version of the place file so I can review the game better? It just makes it easier to review the code and fix any potential errors. I understand if you wouldn’t feel comfortable with this though!

sure, ill send you it in dms.
i hate char limittts

1 Like