Hello! I'm making a game where a random player is chosen as a human, and the others are chosen as plushies. I get the error (stated in the title). Could I get some help with this?
Code:
wait(11) while true do h = Instance.new ("Message") p = game.Players:GetChildren() randplayer = p[math.random(#p)] randplayer.TeamColor = BrickColor.new("Really red") randplayer.Character:BreakJoints() m = Instance.new("Hint") m.Parent = workspace m.Text = randplayer.Name.." was chosen as the Human! " wait(3) m.Parent = nil h.Parent = workspace h.Text = "Plushies! You have 10 minutes to survive!" wait (5) for _, plr in pairs(game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = game.Workspace.bluespawn.CFrame end h.Parent = nil p.Character.HumanoidRootPart.CFrame = game.Workspace.redspawn.CFrame hm = randplayer.Character.Humanoid hm.MaxHealth=300 hm.Health=300 wait(600) for i = 1,#p do if p[i].TeamColor == BrickColor.new("Really red") then p[i].TeamColor = BrickColor.new("Pearl") end end h.Parent = workspace h.Text = "End of game" wait(4) h.Text = "Get ready for the next game" wait (4) h.Parent = nil end
Any working answer is appreciated!
wait(11) while true do h = Instance.new ("Message") p = game.Players:GetChildren() randplayer = p[math.random(#p)] randplayer.TeamColor = BrickColor.new("Really red") randplayer.Character:BreakJoints() m = Instance.new("Hint") m.Parent = workspace m.Text = randplayer.Name.." was chosen as the Human! " wait(3) m.Parent = nil h.Parent = workspace h.Text = "Plushies! You have 10 minutes to survive!" wait (5) for _, plr in pairs(game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = game.Workspace.bluespawn.CFrame end h.Parent = nil randplayer.Character.HumanoidRootPart.CFrame = game.Workspace.redspawn.CFrame hm = randplayer.Character.Humanoid hm.MaxHealth=300 hm.Health=300 wait(600) for i = 1,#p do if p[i].TeamColor == BrickColor.new("Really red") then p[i].TeamColor = BrickColor.new("Pearl") end end h.Parent = workspace h.Text = "End of game" wait(4) h.Text = "Get ready for the next game" wait (4) h.Parent = nil end
Right now you realise that p
is game.Players:GetChildren()
... and game.Players:GetChildren().Character
isn't a thing and will definitely cause an error which is the error you stated
This is extremely difficult to read because of the variable names and the terrible indentation but I think I found the issue. You set "p" as a table of all the players at the start of the loop, but you try to use it like a player on line 21. Instead, loop through every player in the table using a for loop.
One thing to keep in mind is that you shouldn't cache a table of players in a variable, because someone can leave at any time and it would cause an error if you try to access a player that doesn't exist. You should call Players:GetPlayers
or Players:GetChildren
every time you want to do something with every player
for i, player in pairs(game.Players:GetPlayers()) do if player.Character then -- make sure the player has a character so that it won't error if they are respawning or something p.Character.HumanoidRootPart.CFrame = game.Workspace.redspawn.CFrame end end