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

How to detect if this variable is in-game?

Asked by 10 years ago
local Owner = game.Players.ImKittenOuttaMyMind

if --How would I detect for the variable "Owner" to be in the game?

end

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Accessing a child that doesn't exist will throw an error.

We can use FindFirstChild to solve this problem, since instead of throwing an error, it just returns nil if the child is not present.

if game.Players:FindFirstChild("ImKittenOuttaMyMind") then
    -- nil is falsey, so if they are not present, this is fault
    -- all other objects are truey, so if they are present,
    -- this will occur:
    -- At this instant, ImKittenOuttaMyMind is a child of Players
end
0
Thanks! I'm used to scripting in Java, so Lua is a bit confusing! blockhaak2 1 — 10y
Ad

Answer this question