Is this code sustainable for a round system of a "TTT" and "The Stalker" inspired game?

Hey! So I am making a Garry’s Mod inspired game called “Simon’s Sandbox”, and will have multiple gamemodes. Two of them being a “Trouble in Traitor Town” gamemode, and “The Anomaly” gamemode.

I am currently in the process of making a round system though I do not know if this code is sustainable enough or proper. Could someone give me feedback?

Thanks!

function loadCharacters()
	local Players = game.Players:GetPlayers()
	
	for _, player in pairs(Players) do
		player.Team = game.Teams["Neutral"]
		
		if player.Character == nil then
			player:LoadCharacter()
		end
	end
end

function teleportTo()
	local Players = game.Players:GetPlayers()
	local Agents = Teams.Agent:GetPlayers()
	
	local NeutralPlayers = Teams.Neutral:GetPlayers()
	
	for i, v in pairs(NeutralPlayers) do
		v.Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Agent").CFrame
	end
end

function sortTeams()
	canJoin.Value = true
	print(canJoin.Value)
	
	loadCharacters()
	
	wait(5)
	
	canJoin.Value = false
	print(canJoin.Value)
	
	teleportTo()
	
	local Players = game.Players:GetPlayers()
	local NeutralPlayers = Teams.Neutral:GetPlayers()

	if #Players == 12 then
		local index = math.random(3, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	elseif #Players >= 8 and #Players < 12 then
		local index = math.random(2, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	elseif #Players >= 2 and #Players < 8 then
		local index = math.random(1, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	end

	for i, player in ipairs(NeutralPlayers) do
		player.Team = Teams["Agent"]
	end
	
	roundStarted.Value = true
	
	local mainTimer = roundTime
	while mainTimer > 0 do
		wait(1)

		mainTime -= 1
		
		local anomalyActive = false
		local agentsActive = {}
		
		for _, agent in pairs(Teams.Agent:GetPlayers()) do
			if agent then
				table.insert(agentsActive, agent)
			end
		end
		
		for _, anomaly in pairs(Teams.Anomaly:GetPlayers()) do
			if anomaly then
				anomalyActive = true
			end
		end
		
		if #agentsActive < 1 then
			for _, anomaly in pairs(Teams.Anomaly:GetPlayers()) do
				print("give anomaly something idk reward")
			end
			
			mainTimer = 0
			
			break
		end
		
		if not anomalyActive then
			for _, agent in pairs(Teams.Agent:GetPlayers()) do
				print("give agents something idk reward")
			end
			
			mainTimer = 0
		end
	end
	
	roundTime = 0
	
	roundStarted.Value = false
end

while true do
	local skip = false
	
	while intermission > 0 do
		local Players = game.Players:GetPlayers()
		
		if #Players >= 2 then
			intermission = intermission - 1
			print(intermission)
			wait(1)
		else
			while #Players < 2 do
				Players = game.Players:GetPlayers()
				
				wait(1)
			end
			
			skip = true
			
			intermission = 0
		end
	end
	
	wait(1)
	
	if skip == false then
		sortTeams()
		
		while roundTime > 0 do
			wait(1)
		end
	end
	
	skip = false
	intermission = 10
	roundTime = 25
end

It’s very messy with a bit of redundancy.

As an example, this whole block

	if #Players == 12 then
		local index = math.random(3, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	elseif #Players >= 8 and #Players < 12 then
		local index = math.random(2, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	elseif #Players >= 2 and #Players < 8 then
		local index = math.random(1, #Players)
		Players[index].Team = Teams["Anomaly"]
		Players[index].Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
		table.remove(NeutralPlayers, index)
	end

can be replaced by something like

-- Lower and upper bounds for player count
local indexBounds = {[12] = 3, [8] = 2, [2] = 1}

for limit, lowerBound in pairs(indexBounds) do
    if #Players >= limit then
        local index = math.random(lowerBound, #Players)
        local chosenPlayer = Players[index]
        chosenPlayer.Team = Teams["Anomaly"]
        chosenPlayer.Character.HumanoidRootPart.CFrame = workspace:FindFirstChild("Anomaly").CFrame
        table.remove(NeutralPlayers, index)
        break
    end
end

Performance wise it’ll be fine though, it’s only really unsustainable in regards to how easy it’ll be to develop the game further. I’d look into organizing more with modulescripts for various game functions, and then having some sort of gamestates and a main gamemanager script that only controls those.

1 Like

Ah got it, yeah I will probably sort out everything in ModuleScripts in the future. Just gotta get the functions and the main game down first (which sounds unprofessional but idk I do stuff in a different way). Thanks for your reply!

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