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

how to check if a property is there?

Asked by
u_jew 0
6 years ago

I need to check if Text is a value, like

for i,v in pairs(game.Players:GetDescendants()) do
    if v.Text then
        print("text property is here!")
    end
end

but actually works.

2 answers

Log in to vote
1
Answered by 6 years ago
local foundText = false
for i,v in pairs(game.Players:GetDescendants()) do
   if v.Text ~= nil or v.Text ~= "" then
    foundText = true
    print("Text property has a value of: "..v.Text)
   end
end
Ad
Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

If you only needed it for the Text property specifically, you could check if the instance was something that has a Text property (TextLabel,TextBox,TextButton) before referring to the property:

for i,v in pairs(game.Players:GetDescendants()) do
    if v:IsA("TextLabel") or v:IsA("TextBox") or v:IsA("TextButton") then
        print("text property is here!")
    end
end

However, if you are using this to try and alter GUIs through a server script, it will not work. Instead, you might need to use FireAllClients() with a RemoteEvent, and check if it has the Text property in a LocalScript.

Answer this question