If statement problems

I’m trying to make it where when you click on a part, something will happen only if a GUI is open. For example right now I have it where when you click a part a GUI will open. I want to make it where when you click on another part it will only doing something if that GUI is open. I’m kinda new to scripting and needed a little help with this.

Ill just tell you what I have at the moment. So far I have a local script in StarterPlayerScripts:

local gui = game.StarterGui.ScreenGui.ImageLabel

if gui.Visible == true then
	function onClicked()
		print("hi")
	end
	workspace.coin.ClickDetector.MouseClick:connect(onClicked)
end

Then I also have a screen GUI in StarterGui with a local script:

local function VisibilityChange()
	local gui = script.Parent
	gui.Visible = true
end

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(VisibilityChange)

Then I have a part called coin and a part in Workspace. And inside the parts are ClickDetectors and inside the part named part is a script:

local function onClicked(plr)
	game.ReplicatedStorage.RemoteEvent:FireClient(plr)
end

workspace.Part.ClickDetector.MouseClick:Connect(onClicked)

Lastly I have a RemotEvent in ReplicatedStorage.

Too sum it up all I’m trying to do is make it where something will happen when a part is clicked. That certain thing will only happen if a certain GUI is visible. When I click on one part a GUI becomes visible but when I click on the other part nothing happens. Sorry if I made this longer then it should be. Thanks

1 Like

local gui = game.StarterGui.ScreenGui.ImageLabel

replace it with :

local gui = game.Players.LocalPlayer.PlayerGui.ScreenGui.ImageLabel

1 Like

It says "ScreenGui is not a valid member of PlayerGui “Players.naderbocker2.PlayerGui”

Try this :

local gui = game.Players.LocalPlayer.PlayerGui.:WaitForChild("ScreenGui").ImageLabel

1 Like

Also, if you could provide any screenshots

1 Like

So when I click on the grey part a GUI pops up only for the player that clicks it.

But when I click the yellow part nothing happens. Its supposed to print something only if that GUI is visible. It also does not give me any errors.

Screenshot (554)

And do you want all the players in-game to be able to see that UI?

1 Like

No, only the person who clicks on the grey part. But I already coded it where when the player clicks on the grey part a GUI becomes visible. I just want it where when they click on the yellow part something will happen but only if that GUI is visible. Sorry if I’m making it too complicated.

So this code isn’t working for some reason:

local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").ImageLabel

if gui.Visible == true then
	function onClicked()
		print("hi")
	end
	workspace.coin.ClickDetector.MouseClick:connect(onClicked)
end
local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").ImageLabel

function onClicked()
	if gui.Visible == true then
		print("hi")
	end
end

workspace.coin.ClickDetector.MouseClick:Connect(onClicked)
1 Like

It works! Thank you so much. Thanks to everyone else as well.

1 Like