Making a shop GUI appear when player steps in circle

Im trying to make a simple sell/shop area where the player steps into a circle, and then the circle opens the gui for them to sell/buy something.

The problem is, Is when they walk around in the circle it obviously triggers the .Touched event multiple times, so I made a bigger part that covers the shop area and added a .TouchEnded event. But for some reason the touch ended event triggers even when the player is completely inside of the part. Please let me know if theres a better way to make a shop circle, or if there a way to rectify this code. Thank you!

local part = script.Parent
local check = script.Parent.Parent.Outer
local togglegui = game.ReplicatedStorage.ToggleSellGui
local able = true

-- Calls when the player steps on the circle to open the gui
part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player and able then
			local leaderstats = player:WaitForChild("leaderstats")
			
			-- this opens the gui and then stops the player from opening it again
			togglegui:FireClient(player, true)
			able = false
			print("UNABLE TO")
		end
	end
end)


-- this is supposed to call when the player leaves a part that covers the shop area
check.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			able = true
			print("ABLE TO")
		end
	end
end)

3 Likes

If TouchEnded or Touched is inconsistent to use, periodically checking the Character parts if they’re in the Part will do the job.
The basic idea is, Connect the checker in the RunService.Heartbeat event and iterate the player’s character children to check if a basepart is touching the Area part.
If you wanna be performant about it, just check the HumanoidRootPart part only.
An example of mine would be this:

local MainPart = Player.Character.PrimaryPart -- Assuming it's using one Part.
RunService.Heartbeat:Connect(function()
   IsTouching = false
   for i,Part in next,MainPart:GetTouchingParts() do
      if Part == AreaPart then
         IsTouching = true
         break
      end
   end
end)
2 Likes

The triggering of TouchEnded is caused by your characters feet moving up and down on the circle.

A simple fix would be to raise the circle to the characters torso/core level.

2 Likes

Thank you guys both, you kind of helped me realize I shouldn’t be checking for each part of the character so I solved it by checking if the part that touched the sell circle was the humanoid root part so it only triggered once.

2 Likes

No problem, glad to help out a fellow developer.

1 Like