There are two referencing of character that i wrote. One is having the error "attempt to index nil with 'Humanoid'"
1 | local player = game:GetService( "Players" ) |
2 | local localplayer = player.LocalPlayer |
3 | local character = localplayer.Character |
and another one is having this error: Infinite yield possible on 'Players.AriyanHacker29:WaitForChild("Character")
1 | local player = game:GetService( "Players" ) |
2 | local localp = player.LocalPlayer |
3 | local character = localp:WaitForChild( "Character" ) |
any sort of help will be appreciated!!
Hey there! Answer to first one:
1 | local player = game:GetService( "Players" ) |
2 | local localplayer = player.LocalPlayer |
3 | localplayer.CharacterAdded:Wait() |
4 | local character = localplayer.Character |
Character is not a child of player, as far as I'm concerned. So WaitForChild
won't work
and your calling the character before it spawns
The reason why this wouldn't work if because Character
isn't a child of Player
. The player and character do have a connection but are not directly connected. Here are ways to get a player's character:
In a server script:
1 | game.Players.PlayerAdded:Connect( function (plr) |
2 | plr.CharacterAdded:Connect( function (char) |
3 | --char refers to the player's character |
4 | end ) |
5 | end ) |
Local script:
1 | local plr = game.Players.LocalPlayer |
2 | repeat wait() until plr.Character |
3 | local char = plr.Character |
4 |
5 | --events with char being the character |
Is this a server script? Because you cant get LocalPlayer From a server Script.