Hello! I’d like to display the player count in my TextLabel. Any suggestions on how to achieve this?
The current issue is that it shows 0/3 players, even though I’ve already randomly assigned a player to each team. Any ideas on what might be wrong with my script?
ModuleScript:
local TeamsModule = {}
function TeamsModule.new()
local team1 = {
name = "Blue Team",
players = {}, -- You can add more properties as needed
maxPlayers = 3 -- Set the maximum number of players for Team 1
}
local team2 = {
name = "Red Team",
players = {}, -- You can add more properties as needed
maxPlayers = 3 -- Set the maximum number of players for Team 2
}
return team1, team2
end
return TeamsModule
ServerScriptService:
escribe o pega el código aquí
````local TeamsModule = require(game.ReplicatedStorage.TeamsModuleScript) -- Adjust the path according to your structure
-- Get the list of teams created in the module
local team1, team2 = TeamsModule.new()
-- Set the player limits for each team
local maxPlayersTeam1 = team1.maxPlayers
local maxPlayersTeam2 = team2.maxPlayers
-- Function to assign randomly to a team
local function assignToRandomTeam(player)
local randomNumber = math.random(1, 2) -- Generate a random number between 1 and 2
-- Check the random number and assign the player to the appropriate team
if randomNumber == 1 then
if #team1.players < maxPlayersTeam1 then
table.insert(team1.players, player)
print(player.Name .. " added to " .. team1.name .. " team. Players in team: " .. #team1.players)
return team1
else
print(player.Name .. " not added to " .. team1.name .. " team. Team is full.")
return team2
end
else
if #team2.players < maxPlayersTeam2 then
table.insert(team2.players, player)
print(player.Name .. " added to " .. team2.name .. " team. Players in team: " .. #team2.players)
return team2
else
print(player.Name .. " not added to " .. team2.name .. " team. Team is full.")
return team1
end
end
end
-- Function to remove a player from the team
local function removePlayerFromTeam(player)
-- Loop through Team 1's player list and remove the player
for i, teamPlayer in ipairs(team1.players) do
if teamPlayer == player then
table.remove(team1.players, i)
break
end
end
-- Loop through Team 2's player list and remove the player
for i, teamPlayer in ipairs(team2.players) do
if teamPlayer == player then
table.remove(team2.players, i)
break
end
end
end
-- Example of usage in the PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
-- Assign the player to a random team
local assignedTeam = assignToRandomTeam(player)
print(player.Name .. " assigned to " .. assignedTeam.name)
end)
-- Example of usage in the PlayerRemoving event
game.Players.PlayerRemoving:Connect(function(player)
-- Remove the player from the team's player list
removePlayerFromTeam(player)
print(player.Name .. " is leaving the game.")
end)
LocalScript:
-- Local Script
local TeamsModule = require(game.ReplicatedStorage.TeamsModuleScript)
local player = game.Players.LocalPlayer
local team1, team2 = TeamsModule.new()
local blueTeam_textLabel = script.Parent:WaitForChild("blue_Counter").blueTeamText
local redTeam_textLabel = script.Parent:WaitForChild("red_Counter").redTeamText
blueTeam_textLabel.Text = #team1.players .. "/3"
redTeam_textLabel.Text = #team2.players .. "/3"
print("Initial player count in Blue Team: " .. #team1.players)
print("Initial player count in Red Team: " .. #team2.players)