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

Checking if something in the player is there, E.G. character?

If I used for

for i,v in pairs(game.Players:GetPlayers()) do
if v.findFirstChild("Character") then
v.Character:Destroy()
end
end

I'm not actually using that line, but would that work? What's really happening is I have a round script that breaks a lot, so I need to add some if statements to check if the stuff is actually there, or it will break, for example if someone resets during the round, or leaves. Would if v.findFirstChild("Character") then work??

4 comments

0

Your second line is wrong it's supposed to be if v:FindFirstChild("Character") then.Also Character is a property so it can't be destroyed.

0

Other than the error Koz pointed out, that should work.

1

The property is a reference, the reference can be destroyed.

0

Oh yeah the reference is what makes the character like the body parts,Scripts,animations, clothing etc.My bad thanks 1waffle1

1 answer

✓ Accepted answer

Since Character is a property and not an object in Player, it will always be accessible, so not only do you not have to use FindFirstChild but you can't. If you did it would refer to the first child named "Character" if there were one, and not the property.

for _,v in pairs(game.Players:GetPlayers())do
	if v.Character then
		v.Character:Destroy()
		v.Character=nil
	end
end

You may also want to set the Character property to nil so it doesn't still have a reference to a locked object.

1 comment

0

I wasn't trying to destroy the character, I just used it as an example, I was really just asking if FindFirstChild could help not break my round script. Would it?

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