ShopGUI Appearing when Program is Run(It shouldn't be)

Hello,
I’ve recently been learning how to create my own shop originally and so I went with a method to make GUI interfaces show up when a part is touched(the part blends in with the baseplate). Then when contact has been released I wanted to make it so the GUI disappears. However, when I run the program, the game runs the function that I had set only to be run when touched when contact with the part had been made. Here is my code:
local shoppart = script.parent
local shopgui = game.startergui.shopgui
shopgui.shopbackground.visible = false
shopgui.shoptitle.visible = false

shoppart.touched:connect(function()
	shopgui.shopbackground.visible = true
	shopgui.shoptitle.visible = true
	print("contact has been made")
end)

shoppart.touchended:connect(function()
	shopgui.shopbackground.visible = false
	shopgui.shoptitle.visible = false
	print("contact has been released")
end`

return

I know that the issue lies within the touched function because when I commented that function out everything was fine.(except of course it couldn’t work because the function was commented)

2 Likes

You have this script on the server which means that when one person touches it changes it for ALL players instead of just one.

I see a few problems here along with that, first your code should have capitalized events, i.e Touched not touched

Second, you need to have GUI scripts be LocalScripts in the PlayerGui

I have revised the script for you here:

local shopPart = game.Workspace:WaitForChild("shoppart")
local shopGui = script.Parent:WaitForChild("shopgui")

shopGui.shopbackground.Visible = false
shopGui.shoptitle.Visible = false

shopPart.Touched:Connect(function(hit)
    if hit.Parent == game.Players.LocalPlayer.Character then
	     shopGui.shopbackground.visible = true
	     shopGui.shoptitle.visible = true
	     print("contact has been made")
     end
end)

shopPart.TouchEnded:Connect(function(hit)
    if hit.Parent == game.Players.LocalPlayer.Character then
	     shopGui.shopbackground.visible = false
	     shopGui.shoptitle.visible = false
	     print("contact has been released")
     end
end

1 Like

I had the touched and touchEnded events capitalized I think I must have pressed some key to make it look like I didn’t. Thanks for the edit… can you tell me exactly what is happening now in the script so I can understand what is happening. From what it looks like, you added a parameter called hit, but what is hit’s parent. Also, what does the LocalPlayer.Character do? I would appreciate it if you could help me understand. And, I should put all that code in a local script so it only happens for one player when he/she fires the event right?

Put the LocalScript I have made in the StarterGui, where your GUI is.

The script is checking to make sure that the player that touched is the part is the actual player that touched the part. That is why we are using

game.Players.LocalCharacter

To get the character of the LocalPlayer and make sure that the character that hit the part is equal to that value.

And yes, this is why the UI was showing up for everyone instead of one person only since it was a server script.

1 Like

I see… thanks a lot. You explained that very well.

No problem, let me know if anything does not work.

What exactly does WaitForChild() do?

Here is a full thread explanation of what WaitForChild() does;

To shorten it, WaitForChild() checks if the child exists, if it doesn’t, it keeps repeating and yielding until the instance (child) exists.