Gui not always appearing based on player's proximity with Egg Model

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)

Workspace organization:

1 Like

We don’t have a clear understanding as to what your script is doing.
Can you show us it, please?

1 Like

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.

Just replace:

RunService.RenderStepped:Connect(function()

with this code:

Character.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()

This will run every time the character’s HumanoidRootPart’s position changes.

Keep in mind this is just a performance fix. I’m still searching for your main issue.

Is there anything different with the eggs, besides the different mesh? It could be searching for something that doesn’t exist.

I solved the problem by making 2 different RunService connections, one for being within 15 studs and one for not being within 15 studs.

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?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.