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:
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.
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
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)
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)