TouchPart shop gui opening withou touching

So I made a shop gui when you touch a part, but for some reason, when I’m walking, even far from the ShopPart, the GUI opens without me touching, and it even opens before I touch the part

There’s the script:
image

image

You need to check if hit is actually the player’s body part.

local player = game.Players.LocalPlayer
function(hit)
    if hit.Parent == player.Character then
        visibleblablabla
    end
end
1 Like

.Touched detection is not the best option for LocalScripts, you aslo can use magnitudes to check if you are near the shop gui, or Region3, if you wanna keep using .Touched event, i recommend you to do something like this.

.Touched & .TouchEnded events:

game.Workspace.ShopPart.Touched:Connect(function(Hit)
	if Hit:FindFirstChild("Humanoid") then -- Is a player
		local PlayerRoot = Hit:FindFirstChild("HumanoidRootPart") -- Checks for root part
		local Magnitude = (PlayerRoot.Position - game.Workspace.ShopPart.Position).Magnitude -- Checks for magnitude
		if math.modf(Magnitude) <= 10 then -- We check for magnitude (you can change it)
			script.Parent:WaitForChild("Shop").Frame.Visible = true
		end
	end
end)

game.Workspace.ShopPart.TouchEnded:Connect(function(Hit)
	if Hit:FindFirstChild("Humanoid") then -- Is a player aswell
		script.Parent:WaitForChild("Shop").Frame.Visible = false
	end
end)

Region3:

local PlayerPresent = false
local LocalPlayer = game:GetService("Players").LocalPlayer or game:GetService("Players").PlayerAdded:Wait()

while wait() do
	for _,v in pairs(game.Workspace.ShopPart:GetDescendants()) do
		PlayerPresent = false
		local Region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
		local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(Region, LocalPlayer.Character:GetDescendants())
		for _,c in pairs(Parts) do
			if c:FindFirstAncestor(LocalPlayer.Name) then
				PlayerPresent = true
				break
			else
				PlayerPresent = false
			end
		end
		if PlayerPresent == true then
			script.Parent:WaitForChild("Shop").Frame.Visible = true
		else
			script.Parent:WaitForChild("Shop").Frame.Visible = false
		end
	end
end
1 Like

Or you can use Zone+, it’s meant for this purpose.

Yes, that should work aswell, it has a very good touch detection!