Touch a part to open a GUI shop

Hey, so I have been trying to code a part to open up a GUI shop. It’s a red circle on the ground and I want it so when you walk onto it brings up the shop and when you walk away from the part the shop goes away. The ShopButton is in workspace btw. Here is what I have:

image
image
image
image

1 Like

This is the serversctiptservice code:

	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		game.ReplicatedStorage.ShowGUI:FireClient(game.Players:GetPlayers(hit.Parent))
	end
end)

This is the localscript in the shop GUI:

	script.Parent.ShopFrame.Visible = not script.Parent.ShopFrame.Visible
end)

You can use Region3 funcion 'cause, it’s complicated to use TouchEnded Event, so, if you use Region3, you need check if player is in that region. If you wanna use the best form to use Region3, you should to use ZonePlus, 'cause is the best way to use Region3 without a lot of codes.

ZonePlus: ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries - Resources / Community Resources - DevForum | Roblox

Why use an event for something so simple that can be done in client?

I was going off the AlvinBlox video

local HRP:BasePart? = game:GetService("Players").LocalPlayer.Character.PrimaryPart
for _,v in HRP:GetTouchingParts() do
	if tostring(v):lower():match("shopname") then
		game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = true
	else
		game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = false
	end
end
1 Like

Where would I place this script?

You can use my snippet above with replacing shopname as the part name. Or you can use Touched connection.

StarterCharacter is most preferred.

This does not seem to be working.

What seems to be not working?

When I touch the part it still does not pop up the shop GUI

Try that code @develofied, I said ZonePlus 'cause probably you wanna use that Module for other things.

Did you replace shopname with your part name? And are the GUIs variables correct since I just copypasted your code.

I did replace the shopname but I have no idea if the variables are correct. If you look at alvinbloxs’ example that’s what I went off of.

It’s easy to use ZonePlus, literaly you can detect if player is in a espefic zone, like this:

-- This constructs a zone based upon a group of parts in Workspace and listens for when a player enters and exits this group
-- There are also the ``zone.localPlayerEntered`` and ``zone.localPlayerExited`` events for when you wish to listen to only the local player on the client
local Zone = require(game:GetService("ReplicatedStorage").Zone)
local BasePartToDetectRegion = workspace.ShopRegion -- Shop Base part to detect
local zone = Zone.new(BasePartToDetectRegion)

zone.playerEntered:Connect(function(player)
-- When the player enter
    game.ReplicatedStorage.ShowGUI:FireClient(player)
end)

zone.playerExited:Connect(function(player)
-- When the player get out
    game.ReplicatedStorage.ShowGUI:FireClient(player)
end)

Oh my bad, I just realized that loop will only run once.

local HRP:BasePart? = game:GetService("Players").LocalPlayer.Character.PrimaryPart
HRP.Touched:Connect(function()
	for _,v in HRP:GetTouchingParts() do
		if tostring(v):lower():match("shopname") then
			game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = true
		else
			game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = false
		end
	end
end)
--ALT
-- ONLY CHOOSE SECOND PART (ANYTHING BELOW) IF YOU WANNA
local HRP:BasePart? = game:GetService("Players").LocalPlayer.Character.PrimaryPart
local Character = game:GetService("Players").LocalPlayer.Character
HRP.Touched:Connect(function(Part)
	if not tostring(Part.Parent):lower():match(tostring(Character):lower()) and tostring(Part):lower():match("shopname") then
		game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = true
	end
end)
HRP.TouchEnded:Connect(function(Part)
	if not tostring(Part.Parent):lower():match(tostring(Character):lower()) and tostring(Part):lower():match("shopname") then
		game:GetService("Players").LocalPlayer.PlayerGui.Shop.ShopFrame.Visible = false
	end
end)

This does not seem to still work.

You could try something like this.

local RunService = game:GetService("RunService");
local Players = game:GetService("Players");
local Player = Players.LocalPlayer

local PlayerGui = Player.PlayerGui
local Shop = PlayerGui:WaitForChild("Shop");
local ShopFrame = Shop:WaitForChild("ShopFrame");

local MainPart = workspace.Part -- This is the Part that Opens the Shop
local Distance = 7.5

RunService.RenderStepped:Connect(function()
	local Character = Player.Character
	if not Character then
		return
	end
	local HRP = Character:FindFirstChild("HumanoidRootPart");
	if not HRP then
		return
	end
	if (HRP.Position-MainPart.Position).Magnitude < Distance then
		ShopFrame.Visible = true
	else
		ShopFrame.Visible = false
	end
end)

If you want to add multiple Shop Zone Parts you could do this:

local RunService = game:GetService("RunService");
local Players = game:GetService("Players");
local Player = Players.LocalPlayer

local PlayerGui = Player.PlayerGui
local Shop = PlayerGui:WaitForChild("Shop");
local ShopFrame = Shop:WaitForChild("ShopFrame");

local MainParts = {workspace.Part1,workspace.Part2,workspace.Part3} -- This is the Part that Opens the Shop
local Distance = 7.5

RunService.RenderStepped:Connect(function()
	local Character = Player.Character
	if not Character then
		return
	end
	local HRP = Character:FindFirstChild("HumanoidRootPart");
	if not HRP then
		return
	end
	local Magnitude = 999
	for _,v in pairs(MainParts) do
		if (HRP.Position-v.Position).Magnitude < Magnitude then
			Magnitude = (HRP.Position-v.Position).Magnitude
		end
	end
	if Magnitude < Distance then
		ShopFrame.Visible = true
	else
		ShopFrame.Visible = false
	end
end)
2 Likes

Would I place this in the ServerScriptService Script?