I’m trying to, as practice, create a simple round-based sword fighting game. However, the script I’ve made for the round itself can’t seem to grasp that there are 2 or more players in the game to start the round.
Upon joining the game, every player is put into the Spectators team. Every 10 seconds the script attempts to start a round so long as the roundActive variable is false.
Should there be 2 or more players present in the Spectators team, the script should execute the first if statement. Otherwise, the script should execute the elseif statement.
The problem is, when testing via server & clients with 2 clients connected, the script will execute the elseif statement, printing “Round start failed - less than 2 players in the game”.
Posted below is the script itself, why exactly can’t it understand that there are 2 or more players in the game? Thank you!
local roundActive = false
while roundActive == false do
local spectators = game.Teams.Spectators:GetPlayers()
local fighters = game.Teams.Fighters:GetPlayers()
task.wait(10)
print("Attempting round start")
if #spectators >= 2 then
print("2 or more players are present, beginning round start")
game.StarterGui.ScreenGUI.roundCantStart.Visible = false
task.wait(1)
game.StarterGui.ScreenGUI.roundStarting.Visible = true
spectators.team = game.Teams.Fighters
task.wait(5)
fighters.Player:LoadCharacter()
roundActive = true
elseif #spectators < 2 then
print("Round start failed - less than 2 players in the game")
end
end
you should move the get players to within the loop, so it’s updating every time you check the amount of players. Since it’s returning a new table every time you use the function and not updating previous ones. (By this I mean spectators)
Yeah didn’t think of the returned table needing to be updated each loop, thanks for the heads up. I’ve placed those two statements into the loop, however still getting the same output unfortunately
What I usually do in situations like these (where it obviously should be working but its not) is adding print statements throughout the code. Which I see you’ve done, but we can always add some more.
Could you add print(spectators, fighters) right before the if/then statement so we can see what the spectators and fighters actually are represented by in the script?
Also, just looking at your script, another possible reason this doesn’t work could be that it gets the players on different teams before the 10 second wait and then checks for the round after. So if there were 1 player in the game and then another one joined 5 seconds later, then it would still say “not enough players.”
Fetch the spectators again after the task.wait(10) call.
Also spectators.team won’t work, that’s probably something ChatGPT made up. You need a loop that sets the .Team property for each player in the spectators table.
Not ChatGPT, I’m just fairly new to this. However, moving the GetPlayers() statement to after task.wait(10) fixed it! Thank you! I’ll post the updated one here
local roundActive = false
while roundActive == false do
task.wait(10)
local spectators = game.Teams.Spectators:GetPlayers() -- These two statements were previously before task.wait(), thus why it didn't work
local fighters = game.Teams.Fighters:GetPlayers()
print(spectators)
print(fighters)
print("Attempting round start")
if #spectators >= 2 then
print("2 or more players are present, beginning round start")
game.StarterGui.ScreenGUI.roundCantStart.Visible = false
task.wait(1)
game.StarterGui.ScreenGUI.roundStarting.Visible = true
spectators.team = game.Teams.Fighters
task.wait(5)
fighters.Player:LoadCharacter() -- Yeah this didn't work either, lol.
roundActive = true
elseif #spectators < 2 then
print("Round start failed - less than 2 players in the game")
end
end
I’ll have to figure out a way to respawn the players so that they spawn into the arena, maybe I’ll just try setting their health to zero forcing a respawn? We’ll see, thank you!
Thanks, i tried this but got returned "Team is not a valid member of Team “Teams.Fighters” despite the fact that the players team changed to fighters. So it worked, but it breaks the script when this error gets thrown?