3

I have been following many tutorials that use:

game.Players.LocalPlayer.Character

However every time I try to run this I get an error saying:

Players.icrann.PlayerScripts.Script:2: attempt to index field 'Character' (a nil value)

I am trying to use this code to find the HumanoidRootPart so that I can check the position of the player. game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

And also enable the player to run, even if I try use scripts from the toolbox it still wont work. game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 25

In every case it brings up the same error. I also included a screenshot of the Players folder in the Explorer.

Explorer

Thank's in advance for any help.

3 Answers 3

5

I believe you have a timing issue. PlayerScripts execute when the player joins, and it's possible that your Character hasn't loaded yet by the time the script executes.

Move the LocalScript into CharacterScripts, and that will cause the script to fire after the Character has loaded.

1

When your code runs, the player character is not loaded into the game. So you need to add wait.

local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- setting speed
local Humanoid =  character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
1
  • It works however I think @Kylaaa 's way of doing it is a little more simple, Thanks Mar 16, 2020 at 11:48
0

(Local Script Part)

If it was a LocalScript and not a Script u can use If those above didn't work then you can try: repeat wait() until game:IsLoaded() and game:GetService("Players").LocalPlayer.Character on top of the script so it will just wait for the player's character.

(Server Sided Script)

If it was a Script and not a Local Script u can use

for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
if not plr:IsA("Player") then return end
  repeat wait(1) until game:IsLoaded() and plr.Character
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.