Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

4 comments

1

use FindFirstChild()

0

add yield

0

@GoldAngellnDisguise Thank you, it worked. I feel dumb now lol.

1

accept a random answer

1 answer

✓ Accepted answer

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)

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.