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)
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
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?