So I’m having a problem while making a egg shop to buy pets. Whenever a player is within 20 studs of a egg model a gui should pop up, and once the player isn’t in that 15 stud range the Gui should disappear. This only works on one of my egg model even though I am looping through all of them. Any help is appreciated.
LocalScript
--= Roblox Services =--
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--= Dependencies =--
local Convertions = require(ReplicatedStorage:WaitForChild("Convertions"))
--= Constants =--
local ACTIVATION_DISTANCE = 15 -- studs
--= Variables =--
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
--= Object Refrences =--
local EggsFolder = game.Workspace.Map.Zones["ForestZone - Spawn"].Shop.Eggs
local MainGameUI = PlayerGui:WaitForChild("MainGameUi")
local EggOpenFrame = MainGameUI.MainFrames:WaitForChild("EggUi")
--= Initializers =--
RunService.RenderStepped:Connect(function()
for _, EggModel in pairs(EggsFolder:GetChildren()) do
local TargetEgg = EggModel.Name
local MainPart = EggModel.EggMesh:FindFirstChildOfClass("MeshPart")
if (EggModel.EggMesh:FindFirstChildOfClass("MeshPart").Position - Character.HumanoidRootPart.Position).Magnitude <=ACTIVATION_DISTANCE then
if EggOpenFrame.Visible == true then return end
EggOpenFrame.Visible = true
EggOpenFrame.Title.Text = EggModel.Name
EggOpenFrame.Price.Text = Convertions.ToSuffixString(EggModel:GetAttribute("Price"))
elseif (MainPart.Position - Character.HumanoidRootPart.Position).Magnitude > ACTIVATION_DISTANCE then
EggOpenFrame.Visible = false
TargetEgg = nil
MainPart = nil
end
end
end)
My script is under the Localscript tab above? Here:
Script
--= Roblox Services =--
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--= Dependencies =--
local Convertions = require(ReplicatedStorage:WaitForChild("Convertions"))
--= Constants =--
local ACTIVATION_DISTANCE = 15 -- studs
--= Variables =--
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
--= Object Refrences =--
local EggsFolder = game.Workspace.Map.Zones["ForestZone - Spawn"].Shop.Eggs
local MainGameUI = PlayerGui:WaitForChild("MainGameUi")
local EggOpenFrame = MainGameUI.MainFrames:WaitForChild("EggUi")
--= Initializers =--
RunService.RenderStepped:Connect(function()
for _, EggModel in pairs(EggsFolder:GetChildren()) do
local TargetEgg = EggModel.Name
local MainPart = EggModel.EggMesh:FindFirstChildOfClass("MeshPart")
if (EggModel.EggMesh:FindFirstChildOfClass("MeshPart").Position - Character.HumanoidRootPart.Position).Magnitude <=ACTIVATION_DISTANCE then
if EggOpenFrame.Visible == true then return end
EggOpenFrame.Visible = true
EggOpenFrame.Title.Text = EggModel.Name
EggOpenFrame.Price.Text = Convertions.ToSuffixString(EggModel:GetAttribute("Price"))
elseif (MainPart.Position - Character.HumanoidRootPart.Position).Magnitude > ACTIVATION_DISTANCE then
EggOpenFrame.Visible = false
TargetEgg = nil
MainPart = nil
end
end
end)
Instead of running the code every frame ran in the client, you should make the code run every time the player actually moves, so it can then check that position if it’s nearby the eggs or not.
Were you not able to add an if statement, checking if you are nearby or not?
Maybe the line of code where you actually check the distance was incorrectly spelt or coded?