Need help with opening/closing GUI

I have made the shop GUI with in-game currency.
The problem is when someone touches the part that opens GUI, everyone in server can see this GUI and I don’t want that.

Script is LocalScript.

This is my script ^^

Can anyone help me so when someone touches the part, only that person who touched it can see this GUI.

Thanks.

It’s should be in #help-and-feedback:scripting-support

Fixed, thanks for telling me that.

1 Like

Could you paste the code here for us to edit?

edit:
In the mean time, it has to do with your shopMenu() function. Since you did a .Touched event for the function call, you can actually pass an arugment that is the part that touched.

An example, though, is like the following:

function shopMenu(part)
	-- check if part is part of a NPC/player
	if part.Parent:FindFirstChild("Humanoid") then
		-- check if player
		if game.Players:FindFirstChild(part.Parent.Name) then
		
			local Player = game.Players[part.Parent.Name]
		
			-- code
			
		end
	end
end
1 Like

First off we need to put the script in StarterPlayer > StarterCharacterScripts.

The next issue is every time the part is touched we are never checking if it’s our player that touched it, so if something touches it then we are going to open the shop not matter what because we don’t check what/who touched the part.

To fix this we can do

local Character = game.Players.LocalPlayer.Character

local function shopMenu(Hit)
	if canShop and Hit.Parent == Character then
		canShop = false
		Character.Humanoid.WalkSpeed = 0
		frame.Visible = true
	end
end

openPart.Touched:Connect(shopMenu)

edit:
While @Trayeus way can be used, I would go with my way as it can be more reliable at some times mainly because it does not depend on the name of the object.

2 Likes

Either would work. Your’s is more geared toward a LocalScript like the OP wants, but mine can be used on either a LocalScript or a Server Script. It is matter of which way you want to use it. I use this way both locally and on the server-side and have never had an issue with reliability.

By reliability I mean (this does only applies to some games) if your planning on having some AI (aka humanoids/characters or just objects with a humanoid and name) being the names of players then it would register as it being touched. But yes either way would work in this case.

No, this line makes sure that the part belongs to a Player under the game.Players

full code:

Because even if a NPC:

  • Had a Humanoid
  • Was named “Trayeus”

It wouldn’t register as me because it isn’t parented under game.Players.

That worked, thank you very much!
And thank you for all replies, that really helped.

np :+1: glad to see I could help!