I’m not sure why my current script doesn’t work, but I think it has something to do with the humanoid not loading in. I’ve tried adding v.Character:WaitForChild("Humanoid) as the first line inside the for i, v in pairs loop but that did not help. I also tried adding a wait() as the first line of the while loop and as the first line of the for loop. Neither of those seemed to do anything either.
playersService = game:GetService("Players")
teamsService = game:GetService("Teams")
teamFighters = teamsService.Fighters
while true do
if #teamFighters:GetPlayers() == 0 then
if #playersService:GetPlayers() >= 2 then
for i, v in pairs(playersService:GetPlayers())do
v.TeamColor = BrickColor.new("Cocoa")
v:LoadCharacter()
end
end
end
wait(1)
end
This is the error I get when I run the game with 2 clients.
I’ve checked out this topic but their solution didn’t appear to work for me.
You’re correct that it’s detecting two players before their characters are available. A fix would be counting the number of loaded in character instead of just players.
You could try the following function that tests how many characters have loaded in.
function GetLoadedPlayers()
local playersLoaded = 0
for _, player in pairs(playersService:GetPlayers())do
local character = player.Character
if not character or not character.Parent then
print("Character has not loaded in")
else
playersLoaded += 1
end
end
return playersLoaded
end
So your loop becomes
while true do
if #teamFighters:GetPlayers() == 0 then
if GetLoadedPlayers() >= 2 then
for i, player in pairs(playersService:GetPlayers())do
player.TeamColor = BrickColor.new("Cocoa")
player:LoadCharacter()
end
end
end
wait(1)
end
Definitely use a pcall in this loop too so it doesn’t break on any errors.
So I tried your solution (i’ll include a picture incase I implemented it wrong, I sort of went over it in my head and think I understand it). While one of the players loads in, the other play does not. I get the same error message as before.
My plan is to eventually make a king of the hill game. I was going to eventually add a timer but I wanted to fool around with checking players in a team and see if I could use that to respawn the players. Players.CharacterAutoLoads is enabled.
Does removing player:LoadCharacter() from your while loop fix it? I don’t currently see the reason for using this as the script is assuming all characters are loaded to start the game anyway.
I probably should have been more specific, the characters start on the Spectators team. I want them to switch to the fighters team if there are 2 or more players.
Ah ok, I just discovered some functionality of LoadCharacter() you are using that I wasn’t aware of.
I am still unsure of the cause of the issue and it could be an internal Roblox one seeing it’s happening in a core character script (unless you’re using custom player models). However the error is isolated in the Animation script inside the player and therefore shouldn’t interfere with your game’s main while loop.
Are the character models successfully respawning currently when LoadCharacter is called? If so, unless you see any visual issues with player animations, I would leave it for now.
And an alternative to LoadCharacter() would be teleporting each player to their respective spawn point instead. However this would not work if a player is mid-respawning when this happens so you are definitely right to use your current method.
Sorry I can’t be of much more help but I will keep looking!
So I’ve been working more on this. I’ve created sort of an intermission system that respawns players but it slows down when there are more players. The game in progress loop also just shows up and then disappears. Any advice on how to fix my current script or fix my original script would be helpful. Alternatively any information on intermission systems would be helpful.
local Players = game:GetService("Players")
local spectators = 0
local roundInProgress = false
Players.PlayerAdded:Connect(function(plr)
spectators = spectators + 1
end)
while true do
if spectators <= 1 then
for seconds = 30, 1, -1 do
for i, v in pairs(Players:GetPlayers()) do
v.PlayerGui:WaitForChild("Clock")
v.PlayerGui.Clock.TextLabel.Text = ("Intermission "..seconds)
wait(1)
end
end
elseif spectators >= 2 and roundInProgress == false then
roundInProgress = true
for seconds2 = 180, 1, -1 do
for i, v in pairs(Players:GetPlayers())do
v.PlayerGui:WaitForChild("Clock")
v.PlayerGui.Clock.TextLabel.Text = ("Game in Progress "..seconds2)
v.TeamColor = BrickColor.new("Cocoa")
v:LoadCharacter()
wait(1)
end
end
roundinProgress = false
end
wait(1)
end
Okay, so I finally solved this going back and re-creating my system using functions, which took me a couple days to learn more about. I’m glad I did. I’ll post it here incase anyone is interested.
local spectators = 0
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
spectators = spectators + 1
end)
local function startGame()
for i, v in pairs(Players:GetPlayers())do
v.TeamColor = BrickColor.new("Cocoa")
v:LoadCharacter()
end
end
local function startGameTime()
for gameTime = 300, 1, -1 do
for i, v in pairs(Players:GetPlayers()) do
v.PlayerGui:WaitForChild("Clock")
v.PlayerGui.Clock.TextLabel.Text = ("Game time: "..gameTime)
end
wait(1)
end
end
local function startIntermissionTime()
for intermissionTime = 30, 1, -1 do
for i, v in pairs(Players:GetPlayers())do
v.PlayerGui:WaitForChild("Clock")
v.PlayerGui.Clock.TextLabel.Text = ("Intermission: "..intermissionTime)
end
wait(1)
end
end
while true do
if spectators >= 2 then
startGame()
startGameTime()
elseif spectators <= 1 then
startIntermissionTime()
end
wait(1)
end