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

Trying to check if a GUI exists but I always get an error? [Solved, I was dumb rip]

Asked by 5 years ago
Edited 5 years ago

I am trying to see is a GUI exists in PlayerGui, and if not, print that it was not found. I always get this error: ReloadHUD is not a valid member of PlayerGui

This is my code, its a LocalScript inside a tool:

script.Parent.Unequipped:Connect(function()
    out = false
    idle:Stop()
    shootfunction:Disconnect()

    local rhud = game.Players.LocalPlayer.PlayerGui
     if rhud.ReloadHUD then -- If it exists, remove it.
        rhud:remove()
     elseif not rhud.ReloadHUD then -- If its not there print it wasn't found.
        print("noreloadguiwasfound")
    end
end)
1
use FindFirstChild() GoldAngelInDisguise 297 — 5y
0
add yield Wicked_Wlzard 110 — 5y
0
@GoldAngellnDisguise Thank you, it worked. I feel dumb now lol. HeyItzDanniee 252 — 5y
1
accept a random answer WideSteal321 773 — 5y

1 answer

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
5 years ago
Edited 5 years ago

To find if something exists, you should be using :FindFirstChild(). To check a value, a script needs to be able to reference whatever it needs to check, and if the script can't reference what it needs to check, it throws an error because it can't check what you are requesting it to.

It's like if in real life someone told you to go check if their TV was on, but you can't check if their TV is on because they don't have a TV.

Also, don't use :Remove(). That function is deprecated.

script.Parent.Unequipped:Connect(function()
    out = false
    idle:Stop()
    shootfunction:Disconnect() -- disconnects the shooting function.
    local rhud = game.Players.LocalPlayer.PlayerGui
    if rhud:FindFirstChild('ReloadHUD') then -- If it exists, remove it.
        rhud:Destroy()
    elseif not rhud:FindFirstChild('ReloadHUD') then -- If its not there print it wasn't found.
        print("no reload gui was found")
    end
end)
0
Yeah, I know about FindFirstChild. Just completly forgot to use it. Sometimes I just can't think right and I make the most dumb mistakes ever lol. Thank you. HeyItzDanniee 252 — 5y
Ad

Answer this question