Search code examples
luarobloxluau

Roblox Why can't I get Players.LocalPlayer.Character in game


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.


Solution

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