:WaitForChild() doesn't work

Recently when I created a new script, I get this error:


(you can see explorer shows its there)

code1:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local anim = humanoid:LoadAnimation(script:WaitForChild("CA"))
local iscrawlingv = Instance.new("BoolValue")
iscrawlingv.Name = ("iscrawling")
iscrawlingv.Parent = player
local iscrawling = character:WaitForChild("iscrawling")

humanoid.HealthChanged:Connect(function(health)
	if health <= 15 then
		iscrawling.Value = true		
		humanoid.WalkSpeed = 3
		anim:Play()
	elseif health > 15 then
		iscrawling.Value = false
		humanoid.WalkSpeed = 16
		anim:Stop()
	end
end)

code2:

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

local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local distanceMin
local distance
local closestPlayer


UserInputService.InputBegan:Connect(function (input, IsTyping)
	if input.KeyCode == Enum.KeyCode.E and IsTyping == false then
		distanceMin = math.huge
		for _, player in pairs(Players:GetPlayers()) do
			if player ~= LocalPlayer then
				local iscrawling = player:WaitForChild("iscrawling")
				if iscrawling.Value == true then
				distance = player:DistanceFromCharacter(head.Position)
				if distance < distanceMin then
					distanceMin = distance
					closestPlayer = player
				end
			end
		end
	end
		if distanceMin < 10 then 
			print(closestPlayer.Name .. " is the closest player.")
		end
	end
end)
1 Like

You parented the value to player, but your WaitForChild is searching character.

1 Like

Thanks! But this is not the only error about it

image another problem is only one player gets it

That’s because you are only putting one inside of LocalPlayer. If you want to put it in the others you’ll have to loop through all players. Keep in mind though you are in a localscript so the value won’t replicate.

Ok so the first to join is the only one that gets it?

Should I add it with a server script then?

That depends on what you’re planning to use it for, but yes you could go the route of using a server script with PlayerAdded. I’m not really sure what you’re trying to do though.

I’m trying to make a carrying system, if a player “iscrawling/isdown” then they can be carried but the value doesn’t appear

Yeah in that case you will definitely want to use a server script to create the values for the players and to change those values. You can leave your ‘code2’ section dealing with user input and all as a seperate localscript.

hold on, is there a problem with this server script?

game.Players.PlayerAdded:Connect(function(player)
	local iscrawling = Instance.new("BoolValue")
	iscrawling.Name = ("iscrawling")
	iscrawling.Parent = player
end)

it doesn’t seem to work

It worked fine for me, make sure the script is parented in a valid location (I would recommend ServerScriptService as the parent) and not Disabled.