Why does the GUI just open once and doesnt work after touched

Hello, I have a shop in my game that opens when a part is touched. but I have this issue where when I close the GUI I cant open it aain when I touch the part.
I dont know how to fix it, I have tried many different methods to fix it but idk why its doing that.
here is the script that opens the GUI:

game.Workspace.shop.Touch.Touched:Connect(function(hit)
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		
		player.PlayerGui.ShopGui.Frame.Visible = true
	end
end)

and this is the script that closes it:

script.Parent.MouseButton1Up:Connect(function)
        script.Parent.Parent.Visable = false
end)

maybe im just dumb and cant look but pls help.

any help is appreciated!

Show where these scripts are in file explorer

Im assuming when you set the visiblity to false, it is inside of a localscript. Because of this, the server doesn’t realize that the Gui’s visiblity is set to false when you click the button. When the part is touched again, the server still has the Gui’s visibility as true and when you set the value of a property to the same exact value, nothing happens.

It’s better to have all the Gui visibility on the client side and not split apart.

1 Like

as @JustinWe12 said the server doesn’t know if the frame has been set it’s visibility to false.

i believe this is a server-side script.

game.Workspace.shop.Touch.Touched:Connect(function(hit)
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		
		player.PlayerGui.ShopGui.Frame.Visible = true
	end
end)

The thing you should do is creating a RemoteEvent in ReplicatedStorage after that go to a client script and make it a OnClientEvent like this:

local YourRemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("YOUR REMOTE EVENT'S NAME")

YourRemoteEvent.OnClientEvent:Connect(function()
	Player.PlayerGui.ShopGui.Frame.Visible = false
end)

Your ServerSide-Script Should Be Like This:


local YourRemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("YOUR REMOTE EVENT'S NAME")

game.Workspace.shop.Touch.Touched:Connect(function(hit)

	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if humanoid then
		if game:GetService("Players"):FindFirstChild(hit.parent.Name) then
			YourRemoteEvent:FireClient(game:GetService("Players"):GetPlayerFromCharacter(hit.parent))
		end


	end
end)

It seems like there’s a small typo in your closing script. Instead of Visable , it should be Visible .
Here’s the edited one.

script.Parent.MouseButton1Up:Connect(function()
    script.Parent.Parent.Visible = false
end)

Additionally, to ensure that the GUI can be opened again after closing, you might want to consider resetting its visibility when the player touches the part again. You can do this by setting the visibility of the GUI to true when the part is touched. Here’s the modified opening script:

game.Workspace.shop.Touch.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    
    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
        
        player.PlayerGui.ShopGui.Frame.Visible = true
    end
end)

and i think the typo might be the problem.

maybe try and use my code as a baseline, and work your way through the code and find a way to improve it to work with the original code.

I mean there is a typo but there is a second problem that i fixed in my post.

1 Like

oh. I’ll check it out. Can i know what the error is?

1 Like

okay, the error in this code is: The OP uses a serverscript to make the frame visible. when the frame is made invisible in a client script the server doesn’t know that it has been made invisible so it doesn’t become visible in result.

if the visibility of the GUI is controlled by a server script, then changes made to it need to be replicated to the client. Here’s how you can modify your scripts to ensure that the visibility changes are properly synchronized between the server and the client.

Opening Script (Server Script):

game.Workspace.shop.Touch.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    
    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
        if player then
            -- Fire a remote event to inform the client to open the GUI
            game.ReplicatedStorage.OpenShopGui:FireClient(player)
        end
    end
end)

Closing script (Client sided):

-- Assuming this script is located within the GUI frame
script.Parent.MouseButton1Up:Connect(function()
    -- Fire a remote event to inform the server to close the GUI
    game.ReplicatedStorage.CloseShopGui:FireServer()
end)

Server Script (to handle closing on the server):

-- Assuming this script is a ServerScript
local replicatedStorage = game:GetService("ReplicatedStorage")
local closeShopGui = replicatedStorage:WaitForChild("CloseShopGui")

closeShopGui.OnServerEvent:Connect(function(player)
    -- Assuming the GUI is parented to the player's PlayerGui
    local gui = player.PlayerGui:FindFirstChild("ShopGui")
    if gui then
        gui.Frame.Visible = false
    end
end)

Client Script (to handle opening on the client):

-- Assuming this script is a LocalScript
local replicatedStorage = game:GetService("ReplicatedStorage")
local openShopGui = replicatedStorage:WaitForChild("OpenShopGui")

openShopGui.OnClientEvent:Connect(function()
    -- Assuming the GUI is parented to the player's PlayerGui
    local gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("ShopGui")
    if gui then
        gui.Frame.Visible = true
    end
end)

That script would work too! aa

if there’s any error still, i will not be able to fix it because it’s night at my time and i really need to sleep.
Maybe tommorow ill fix the error if there is one.

Well, there is a little error, I guess. The error is that you are not supposed to handle gui’s visibility on the server because if you fire the server in the client and make the gui invisible, it will make all of the players gui’s invisible.

Maybe i could fix that error. You’re right, though. Handling the GUI visibility directly on the server can indeed affect all players’ GUIs.
So i think what you need to do here is within all these things.
And this just tells what to add.

  1. Server Script (Opening):
  • When the part is touched, use a RemoteEvent to notify the client to open the GUI.
  1. Client Script (Opening):
  • Listen for the RemoteEvent fired by the server.
  • When received, make the GUI visible.
  1. Client Script (Closing):
  • Inside the GUI frame, when the close button is clicked, use another RemoteEvent to inform the server to close the GUI.
  1. Server Script (Closing):
  • Listen for the RemoteEvent fired by the client to close the GUI.
  • Upon receiving, use a RemoteEvent to inform the client to update the GUI’s visibility.
  1. Client Script (Visibility Update):
  • Listen for the RemoteEvent fired by the server to update GUI visibility.
  • When received, adjust the GUI’s visibility accordingly.

hope this helps, im gonna go sleep now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.