GUI not opening and closing multiple times

I am trying to make a fridge that opens a gui when you trigger a proximity prompt, and closes the gui when you press a button within the gui. The code I have will allow it to open and close once, but it won’t work multiple times. There are no errors that show up in the output window. Here is the code:
Open script:

local fridgePrompt = script.Parent.FridgePrompt

fridgePrompt.Triggered:Connect(function(plr)
	local PlayerGui = plr:WaitForChild("PlayerGui")
	local fridgeGui = PlayerGui.FridgeGUI.FridgeGUIFrame
	fridgeGui.Visible = true
end)

Close script:

local button = script.Parent
local fridgeGui = button.Parent

local function closeFridgeGui()
	fridgeGui.Visible = false
end

button.Activated:Connect(closeFridgeGui)
1 Like

Just to be clear, your problem is after it has been opened and closed once, it doesn’t seem to be able to open it again?

1 Like

Try something like this for the prox prompt (script):

local fridgePrompt = script.Parent.FridgePrompt
local open = false

prompt.Triggered:Connect(function(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	local fridgeFrame = PlayerGui.FridgeGUI.FridgeGUIFrame
	if open == false then
		open = true
		fridgeFrame.Visible = true
	else
		open = false
		fridgeFrame.Visible = false
		
	end
end)

Then for the close button (local script):

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	player.PlayerGui.FridgeGUI.FridgeGUIFrame = false
end)

Let me know if you have any other issues.

1 Like

Client-server issue.

You’re setting the player’s gui’s visibility to true from the server and to false from the client, that change made by the client won’t replicate to the server so from the server’s perspective the gui is visible even if it was set to invisible by the client.

You can either use a RemoteEvent instance to circumvent your issue (which I recommend) or do the following.

fridgePrompt.Triggered:Connect(function(plr)
	local PlayerGui = plr:WaitForChild("PlayerGui")
	local fridgeGui = PlayerGui.FridgeGUI.FridgeGUIFrame
	fridgeGui.Visible = false
	fridgeGui.Visible = true
end)
1 Like

What is a RemoteEvent and why would you recommend it over the other solution you gave?

The solution I provided is hacky but very simple, don’t worry though, it won’t cause any issues down the line.

You’re welcome to read the following resources on RemoteEvents/RemoteFunctions for more information.

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

2 Likes