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

How is this a 'nil value'? I put 'if' so it should ignore it if it's nil

I'm getting an error saying 'attempt to index a nil value' Something is wrong with this bit of code, but I have no idea what;

for i,v in pairs(plrs:GetChildren()) do
	if v.Character and v.Character.HumanoidRootPart then v.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-169.21, 5.044, 11.959))
	end
end

It happens when the character has fallen down into the emptiness (the FallenPartsDestroyHeight).

2 answers

for i, v in pairs(game.Players:GetChildren()) do 
	if v:IsA("Player") and v.Character then 
		local character = v.Character
		if character.Humanoid then 
			if character.Humanoid.Health > 0 then 
			--code
			end
		elseif not character.Humanoid or character then 
			print("character is nil")
		end
	end
end

1 comment

0

First of all, don't use game.Players:GetChildren(), use GetPlayers() instead. Second of all, if there's no humanoid then using "character.Humanoid", even inside an if statement, will cause an error. Third of all, your answer is in no way related to the op's question

Try using FindFirstChild() instead.

Also, use Players:GetPlayers(), not Players:GetChildren()

You can do it like this:

for i,v in pairs(plrs:GetPlayers()) do
	if v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
		v.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-169.21, 5.044, 11.959))
	end
end

Or like this:

for i,v in pairs(plrs:GetPlayers()) do
	local hrp = v.Character and v.Character:FindFirstChild("HumanoidRootPart")
	if hrp then
		hrp.CFrame = CFrame.new(Vector3.new(-169.21, 5.044, 11.959))
	end
end

1 comment

0

Thanks, I figured it out though. It was still showing the error from the play test, and I was using the local server afterwards so it didn't update the output.

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