Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 10 years ago

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??

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. Kozero 120 — 10y
0
Other than the error Koz pointed out, that should work. Shawnyg 4330 — 10y
1
The property is a reference, the reference can be destroyed. 1waffle1 2908 — 10y
0
Oh yeah the reference is what makes the character like the body parts,Scripts,animations, clothing etc.My bad thanks 1waffle1 Kozero 120 — 10y

1 answer

Log in to vote
2
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

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.

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? systematicaddict 295 — 10y
Ad

Answer this question