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

Error while checking for null object?

while wait(1) do
    local pos = Vector3.new(0, 0, 0)
	for _, plr in pairs(Game:GetService("Players"):GetPlayers()) do
        if plr.Character.Humanoid then
            plr.Character.Humanoid:MoveTo(pos)
end
end
end

So i'm making everyone walk to a point on the map, but when a player has no humanoid, the script gives an error and stops running (Also yes, i don't want to tp them, i'm making them walk)

Now this doesn't make sense as it checks for the humanoid and if it's there it runs below it, any way to fix this error?

2 comments

0

fortnite

0

what's the error?

1 answer

✓ Accepted answer

Problem

Error while checking for null object?

What Wrong

Roblox generates an error when you try to index an invalid child of an instance.

Solution

Fortunately, they have a function you can use to combat this. Its called :FindFirstChild and its a method that works like :WaitForChild but instead of waiting it just finds the object upon calling and returns nothing if it couldn't find a child with the given name.

Example Code

This code will check if the character exists, then if the Humanoid does. If those pass then it will move them to pos

local character = player.Character

if character then
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		humanoid:MoveTo(pos)
	end
end

1 comment

0

Thank you

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