I’m making a game for my cousin about two teams fighting each other in a open world. I want the teams to be balanced so it’s not unfair, so I made a script that blocks people from joining a team that has too much players, but the script doesn’t work and lets people in any team they want, and I don’t know what’s causing the issue.
Here’s the script:
local RepStorage = game:GetService("ReplicatedStorage")
local RemoteEventFolder = RepStorage:WaitForChild("RemoteEvents")
local SpawnEvent = RemoteEventFolder:WaitForChild("SpawnEvent")
local spawnpart = workspace.SpawnPart
local teams = game:GetService("Teams")
local redteam = teams:WaitForChild("Red")
local blueteam = teams:WaitForChild("Blue")
local redplayers = redteam:GetPlayers()
local blueplayers = blueteam:GetPlayers()
SpawnEvent.OnServerEvent:Connect(function(player, chosenteam)
local character = player.Character
if chosenteam == "red" then
if #redplayers <= #blueplayers then
player.Team = redteam
character.HumanoidRootPart.Position = spawnpart.Position
elseif #redplayers > #blueplayers then
print("red team is full")
end
end
if chosenteam == "blue" then
if #blueplayers <= #redplayers then
player.Team = redteam
character.HumanoidRootPart.Position = spawnpart.Position
elseif #blueplayers > #redplayers then
print("blue team is full")
end
end
end)
Help appreciated, thanks!
I think the issue is that your :GetPlayers() lists aren’t being updated every time the event fires. Try moving those two lines into the event, so those numbers update every time the event fires.
Hope this helps,
Warrior
I would implement the blocking onto the client side, not when they already clicked the team they wanted to join into the server.
Assuming you are using a TextButton instance then just disable it if there is too many players on one side then the other.
Here’s an example. (Assuming there is just only going to be two teams) and put this where the
team selecting ui is at (local script obviously) and add ur own implementation where the comments are.
just add whatever is missing into the checkTeams functions
(the main reason why you would implement into client side is to indicate that teams are full and it’s just a lot easier to implement)
local Team = game:GetService("Teams")
local RedTeamPlrs = #Team.Red:GetPlayers()
local BlueTeamPlrs = #Team.Blue:GetPlayers()
local function checkTeams()
if RedTeamPlrs > BlueTeamPlrs then
-- // disable blue team button, re-enable red button
elseif RedTeamPlrs < BlueTeamPlrs then
-- // disable red team button, re-enable blue button
else
-- // enable both buttons
end
end
Team.Red.PlayerAdded:Connect(function()
RedTeamPlrs += 1
checkTeams()
end)
Team.Red.PlayerRemoved:Connect(function()
RedTeamPlrs -= 1
checkTeams()
end)
Team.Blue.PlayerAdded:Connect(function()
BlueTeamPlrs += 1
checkTeams()
end)
Team.Blue.PlayerRemoved:Connect(function()
BlueTeamPlrs -= 1
checkTeams()
end)
Hey!
My solution has been solved already by @Warriorgamer7777 , sorry.
Although indicating that the team is full may actually be a good idea, however,
Thanks for helping though! 