Looking for improvements and suggestions on my round system

What does code do?

The code works by making several functions that work as steps to make the round, and then restarting it by calling the repeat function to look for players.

Not satisfied because?

I’m not satisfied with it because it feels strange that I have to do it once after every function and then it’s infinite forever.

Considered improvements?
I’ve considered making it more controlled by not making the very start of my countdown in 1 task.spawn()

How do I want to improve code?

I’m not sure how I want to improve this code but I feel like this system could be more optimized.

Main script:

local start = 30
local ending = 0
local increase = -1


function countdown()
	game.ReplicatedStorage.CountdownEvent:FireAllClients(start,ending,increase)
	game.ReplicatedStorage.StopEvent.OnServerEvent:Connect(function(plr,text)
		if text == "0" then
			local char = plr.Character
			char:MoveTo(workspace.Map.Position + Vector3.new(math.random(-12,12),5,math.random(-12,12)))
			game.ReplicatedStorage.PlayingEvent:FireAllClients(start,ending,increase)
		end
	end)
end

function runningup()
	while task.wait() do
		local players = game:GetService("Players")
		repeat
			task.wait(1)
		until #players:GetPlayers() >= 1
		if #players:GetPlayers() >= 1 then
			task.spawn(countdown)
			break
		end
	end
end

game.ReplicatedStorage.RestartEvent.OnServerEvent:Connect(function(plr, text)
	if true and text == "0" then
		local char = plr.Character
		char:MoveTo(workspace.Lobby.Position + Vector3.new(0,5,0))
		task.spawn(runningup)
	end
end)
task.spawn(runningup)

Client-side script regarding the GUI:

local text = script.Parent
game.ReplicatedStorage.CountdownEvent.OnClientEvent:Connect(function(start,ending,increase)
	for i = start,ending,increase do
		text.Text = tostring(i)
		task.wait(1)
	end
	game.ReplicatedStorage.StopEvent:FireServer(text.Text)
	text.Text = "Play!"
end)
game.ReplicatedStorage.PlayingEvent.OnClientEvent:Connect(function(start,ending,increase)
	for i = start,ending,increase do
		text.Text = tostring(i)
		task.wait(1)
	end
	game.ReplicatedStorage.RestartEvent:FireServer(text.Text)
	text.Text = "Intermission or waiting for players.."
end)
3 Likes

You should use more variables; like for the remote events. Also add more spacing between the lines of code.

More specifically here

Also if true is useless. Definitely can improve optimization. But I’m too lazy so I’ll just help with how your code looks. :wink:

4 Likes