Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my character script not working and showing errors?

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

✓ Accepted answer

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

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

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

1 comment

0

Nevermind i misread that, But you cant get localplayer from a serverscript if your trying.

This discussion is preserved as it was. New answers, comments, and votes are closed.