Gui only opens once

Hello, I am having trouble with opening a shop gui. But the problem is that it only opens once, here are the scripts:

This script puts the gui inside of the player gui

local clonedGui = script.Parent:Clone()

local gui = script.Parent

if gui.Parent == game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui") then
print("Gui already in playergui")
else
clonedGui.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")

print("Gui put in playergui")

end

And this script opens it

script.Parent.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then 
		game:GetService("Players"):FindFirstChild(hit.Parent.Name).PlayerGui.ItemAndRebirthShop.Frame.Visible = true 
	end
end)

Thank You

1 Like

When it opens, do you close the GUI using its button inside I mean is does the close button have functionality using a localscript?

Yes, it does use a local script

The code is not efficient and you are handling GUI on server (laggy) . The reason for not working is you are changing its Visibility to true then never to False. Now , put the second script in a local Script inside StarterPlayer or somewhere and add a Touched and TouchEnd Event , once touched check if the Touched Character is the Local Player’s Character and then change the Visibility of the GUI to true. Once TouchEnded check if the Touched Character is the Local Player’s Character and then change the Visibility of the GUI to false or you can make it based on toggle.

1 Like

Here your issue, You can only open it once because when you close it clientside, the server will still think it’s open.

Example: How do you close your GUI?

EDIT: The best way to do what you are looking for is doing it all Clientside.

Here a small script serverside.
Need to be a Localscript and under the ShopGUI.
ShopGUI should be in StarterPlayerGui (Or at least in the player UI by default)


local Shop = --// PUT THE PART THAT PLAYER NEED TO TOUCH TO OPEN THE SHOP
local Bool = false

Shop.Touched:Connect(function(hit)
     local Player = game.Players:GetPlayerFromCharacter(hit.Parent:FindFirstChildWhichIsA("Humanoid"))
     if Player and Player == game.Players.LocalPlayer then
           if not Bool then
               Bool = true
               Script.Frame.Visible = true
            end
      end
end)

script.Parent.Frame.ExitButton.MouseButton1Click:Connect(function() --// PUT EXIT BUTTON
    Bool = false
    Script.Frame.Visible = false
end)

That would open the GUI if any part touched Shop’s Parent.

1 Like

True, let me do some edits, i frogot that.

EDITED

Hello, Thank you for your help but every time I test the game the output says that “Touched” isn’t apart of the workspace (Line 4)

Try to use Shop.Touched instead of Shop.Parent.Touched

1 Like

Edited, i frogot to remove a Parent, thanks for @caviarbro for find my mistake.

1 Like