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