Issue getting character from player

Title

local player = game.Players.LocalPlayer
local character = player.Character
local head = character:FindFirstChild("Head")

while wait(.1) do
	local mag = (head.Position - game.Workspace.Part.Position).Magnitude
	script.Parent.Text = "Distance to part: "..mag
	if mag < 10 then
		character.Humanoid.Health = 0
	end
end

Im pretty sure its an error getting character

error; [Players.topcst.PlayerGui.ScreenGui.Frame.TextLabel.LocalScript:6: attempt to index nil with ‘Position’]

You may want to use WaitForChild instead of FindFirstChild, because it’s not going to load instantaneously. Same for the character, but you’d use CharacterAdded:Wait():

local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")

Also, don’t use wait as a condition for a while loop

You would be right, head seems to be returning nil as it is can’t find the child, try doing something like this:

local head = character:FindFirstChild("Head") or character:WaitForChild("Head");

And a quick recommendation, while wait do is very inefficient, you should use run service in the case of this loop.

local RunService = game:GetService("RunService");
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local head = character:FindFirstChild("Head") or character:WaitForChild("Head");

RunService.Heartbeat:connect(function()
	local mag = (head.Position - game.Workspace.Part.Position).Magnitude
	script.Parent.Text = "Distance to part: "..mag
	if mag < 10 then
		character.Humanoid.Health = 0
	end
end)

Thank you both for replies. Im relatively new to scripting and new that while wait() do is bad practise but didnt know if there was a way to see if user has moved, Ty :slight_smile:

This is an issue with the way you’re fetching Character, it usually takes a second to load a Character after the Player joins the game, this is why there’s a Player.CharacterAdded() event.

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

function characterAdded(character)
     local head = character:WaitForChild("Head") --WaitForChild instead of FindFirst since parts don't load simultaneously.
     while character do wait(0.1)
          local mag = (head.Position - game.Workspace.Part.Position).Magnitude
	      script.Parent.Text = "Distance to part: "..mag
	      if mag < 10 then
		       character.Humanoid.Health = 0
	      end
     end
end

player.CharacterAdded:Connect(characterAdded)
if player.Character then characterAdded(player.Character) end

Don’t use RenderStepped - Hearbeat is a better event. You shouldn’t use RenderStepped if you don’t want to update anything before a frame renders. It is used for when you want to update the player’s position or the camera - this’ll like cause performance issues

1 Like

Okay, is what is difference between findfirstchild and waitforchild?

Is it that FindFirstChild just “looks” the once for the value
and WaitForChild can “look” unlimited times until it has loaded in

FindFirstChild() checks once for a child with the given name, WaitForChild() will not let code progress until it find a child with the given name.

Replying to @WingedDash and @Raretendoblox:

RenderStepped/Heartbeat are both not a good idea to use here, this would actually slow down the game with totally unneeded updates every frame. The GUI only needs to update every so often, not every frame, in this case there’s no need for that level of precision.