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
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