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)
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.
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")
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)
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)
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)
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.
Server Script (Opening):
When the part is touched, use a RemoteEvent to notify the client to open the GUI.
Client Script (Opening):
Listen for the RemoteEvent fired by the server.
When received, make the GUI visible.
Client Script (Closing):
Inside the GUI frame, when the close button is clicked, use another RemoteEvent to inform the server to close the GUI.
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.
Client Script (Visibility Update):
Listen for the RemoteEvent fired by the server to update GUI visibility.
When received, adjust the GUI’s visibility accordingly.