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 3 years ago

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

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

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

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

any sort of help will be appreciated!!

3 answers

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

Hey there! Answer to first one:

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

Ad
Log in to vote
0
Answered by 3 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:

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

Local script:

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character

--events with char being the character
Log in to vote
0
Answered by 3 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 — 3y

Answer this question