Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my character script not working and showing errors?

Asked by 4 years ago

There are two referencing of character that i wrote. One is having the error "attempt to index nil with 'Humanoid'"

1local player = game:GetService("Players")
2local localplayer = player.LocalPlayer
3local character = localplayer.Character

and another one is having this error: Infinite yield possible on 'Players.AriyanHacker29:WaitForChild("Character")

1local player = game:GetService("Players")
2local localp = player.LocalPlayer
3local character = localp:WaitForChild("Character")

any sort of help will be appreciated!!

3 answers

Log in to vote
0
Answered by
iHavoc101 127
4 years ago
Edited 4 years ago

Hey there! Answer to first one:

1local player = game:GetService("Players")
2local localplayer = player.LocalPlayer
3localplayer.CharacterAdded:Wait()
4local 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

Ad
Log in to vote
0
Answered by 4 years ago

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:

1game.Players.PlayerAdded:Connect(function(plr)
2    plr.CharacterAdded:Connect(function(char)
3        --char refers to the player's character
4    end)
5end)

Local script:

1local plr = game.Players.LocalPlayer
2repeat wait() until plr.Character
3local char = plr.Character
4 
5--events with char being the character
Log in to vote
0
Answered by 4 years ago

Is this a server script? Because you cant get LocalPlayer From a server Script.

0
Nevermind i misread that, But you cant get localplayer from a serverscript if your trying. EllaTheFloofyFox 106 — 4y

Answer this question