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

“attempt to index nil with ‘HumanoidRootPart’”?

Asked by 3 years ago

Trying to teleport all players for a star wars shooter game. The following function gives the error attempt to index nil with 'HumanoidRootPart'.

Any help?

(I have already tried a WaitForChild, that returned the same error but with WaitForChild rather than HumanoidRootPart.)

for _, player in pairs(game.Players:GetChildren()) do
        if player.Team ~= game.Teams.Lobby then
            player.Character.HumanoidRootPart.CFrame = Spawns[math.random(1,#Spawns)].CFrame
            player.Character.Humanoid.WalkSpeed = 0
        end
    end
0
Try using "game.Players:GetPlayers()" There may be something in "game.Players" that isn't a player Fad99 286 — 3y
0
Just a suggestion, instead of using HumanoidRootPart.CFrame use Character:SetPrimaryPartCFrame("Insert CFrame Here") Oblivious_Blitz 1 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Chances are your script runs when the player's character still not in game, try to wait until character the player's character exist

for _, player in pairs(game.Players:GetChildren()) do
        if player.Team ~= game.Teams.Lobby then
        Character = workspace:WaitForChild(player.Name)            
        Character.HumanoidRootPart.CFrame = Spawns[math.random(1,#Spawns)].CFrame
            Character.Humanoid.WalkSpeed = 0
        end
    end
Ad

Answer this question