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

BodyPosition is not a valid member of Part?

So I have this loop to position a pet behind a player

while true do
	wait()
	if Pet.Driver.BodyPosition then
		Pet.Driver.BodyPosition.Position = --vector 3 postion thing
	end
end

But when I request for the Pet to be Destroyed as the player has unequipped it, it appears with that error as stated in the title, but surely the IF statement would prevent this? The code continues to run but I would prefer to not have the error every time the player does this.

1 answer

✓ Accepted answer

There will be an error if you try to check for a child without FindFirstChild or WaitForChild. You should use this instead:

while true do
    wait()
    if Pet and Pet:FindFirstChild("Driver") and Pet.Driver:FindFirstChild("BodyPosition") then --If everything to get to the BodyPosition is there, it won't error out while doing this.
        Pet.Driver.BodyPosition.Position = --vector 3 postion thing
    end
end

EDIT:

If you call :Destroy() on the pet, your script could just check if Pet is existent.

while true do
    wait()
    if Pet then --If the pet is there, it will continue.
        Pet.Driver.BodyPosition.Position = --vector 3 postion thing
    end
end

5 comments

0

But wouldn't using :FindFirstChild be taxing on the games performance?

0

It is the proper way to check if something is there, you can delete the last one if Driver is destroyed with the pet or even just Pet if the pet is destroyed.

0

The script doesn't throw an error when I use a loading variable and a wait(1), but preferably I'd like to use this instantaneously

0

Please check my new edit, I'm telling you the proper way to do it whether or not you feel you like it.

0

i understand :(

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