Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

“attempt to index nil with ‘HumanoidRootPart’”?

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

2 comments

0

Try using "game.Players:GetPlayers()" There may be something in "game.Players" that isn't a player

0

Just a suggestion, instead of using HumanoidRootPart.CFrame use Character:SetPrimaryPartCFrame("Insert CFrame Here")

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.