Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Workspace.gamehandler:21 attempt to index nil with 'HumanoidRootPart'?

Asked by
2_MMZ 1059 Moderation Voter
3 years ago

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!

1
Okay first off I suggest you make your variables more concise in their purpose. Instead of doing "p" do "players". That just makes it more readable not only for other people, but for yourself as well. acer1102 97 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago
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

0
i'm assuming that the p you mentioned is the human btw Gmorcad12345 434 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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:GetPlayersor 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
0
This gives me the same error 2_MMZ 1059 — 3y
0
looks like I made a typo. it should be "player.Character.HumanoidRootPart.CFrame" and not "p.Character.HumanoidRootPart.CFrame" OfficerBrah 494 — 3y

Answer this question