How could I shorten my round-based game code?

There are two different modes in my game: Free for all and Team Deathmatch.
Is there a way to use a module script or someone other way to shorten my code in my main game script?

Heres the Free for all part:

local ChosenGameMode = AvailableGameModes[math.random(1,#AvailableGameModes)]
Status.Value = "Choosing a GameMode!"
wait(1)
Status.Value = ChosenGameMode.Name
if ChosenGameMode.Name == 'Free-For-All'  then
	require(script.Parent.Parent.Parent.ServerStorage.GameModes.ModuleScript)

	 local plrs = {}
        	
        	for i, player in pairs (game.Players:GetPlayers()) do
		  	if (not player.IntroTag.Value) and (not player.AFK.Value) then 
    			table.insert(plrs,player)--Add each players into plrs table
				print(player)
				print(#plrs)
		     else
				warn(player, 'was not Inserted - AFK or Intro.')
				print(#plrs)
							
		end 
	end


	wait(1)

local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

Status.Value = "Choosing a Map..."
wait(3)

Status.Value = ChosenMap.Name.." Chosen"
wait(1)	
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace

	



--[[      |				|
		  |				|
	 Teleport Players to Map
		  |				|
		  |				|
							
						--]]
wait(1)
workspace.LobbyMusic.Intermission:Pause()
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

if not SpawnPoints then 	
	error("SpawnPoints not Found!")
end

pcall(function()
local AvailableSpawnPoints = SpawnPoints:GetChildren()

for i, player in pairs(plrs) do
	if player then
		character = player.Character
		
		if character then 
			--Teleport Them  			
			character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,8,0)
			table.remove(AvailableSpawnPoints,1)
			
			
			--Give them a sword
			local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
			
			if equipped.Value ~= "" then
				local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
				weapon.Parent = player.Backpack
			else
				local Sword = ServerStorage.Sword:Clone()
				Sword.Parent = player.Backpack
			end
			game.Workspace:FindFirstChildOfClass("Player")
						
			local GameTag = Instance.new("BoolValue")
			GameTag.Name = "GameTag"
			GameTag.Parent = player.Character
		else
			--There is no Character 
			if not player then 
				table.remove(plrs,i)
			end
		end
	end
end	
end)

Status.Value = "Get Ready to Play!"

wait(3)

for i = GameLength,0,-1 do 
	
	for x, player in pairs(plrs) do
		if player then
			
			character = player.Character
			
			if not character then 
				--left the game
				table.remove(plrs,x)
			else
				if character:FindFirstChild("GameTag") then 
					--They are still alive
					print(player.Name.." is still in the game!")
				else
					--They are dead
					table.remove(plrs,x)
				end
			end
		else 
			table.remove(plrs,x)
			print(player.Name.." has been Removed!")
			
		end
	end
	
	Status.Value = "Free-For-All|Time:"..i.."|Players:"..#plrs..""

I’d move this to #help-and-feedback:code-review

1 Like

Since this is about reviewing code then I would recommend moving this post over to #help-and-feedback:code-review for the appropriate feedback you’re looking for.

With that said, it really all up to you whether you are trying to shorten it up or not. Deciding what to put or not to must in a ModuleScript is all up to personal preference and/or whether it would make logical sense put it in there.

I don’t think there’s enough lines to determine how to improve this code, unless somebody more experience than me has a better take.

I just think that the indentation could use some work.

1 Like

local ChosenGameMode = AvailableGameModes[math.random(1,#AvailableGameModes)]

You can shorten this by removing the local.

part = game.Workspace.Part is the same as local part = game.Workspace.Part.

Don’t do that! It’s standard for all variables to be locally scoped. Not only its technically faster to access, its to prevent it having collisions with other scopes. A global variable is always a no-no.

1 Like

Oof, I’m still learning how to code, I still make extremely horrible mistakes. :slight_smile:

1 Like

We all began from somewhere, don’t worries lol