My egg Info Frame isn't working?

Hi! I have two eggs that when you get close to it a gui pops up telling you all the eggs that you can buy. With one egg it works fine, but with two eggs it doesn’t work. Please tell me what I have done wrong.

Local Script:

game:GetService("RunService").RenderStepped:Connect(function()
	for i, v in pairs(workspace:WaitForChild("Eggs"):GetChildren()) do
			local char = plr.Character or plr.CharacterAdded:Wait()
			local Magnitude = (v.Egg.Position - char:WaitForChild("HumanoidRootPart").Position).magnitude
			
			if Magnitude <= 8 then
					if plr.PlayerGui.MainGUI.EggFrame.Visible == false then
						plr.PlayerGui.MainGUI.EggFrame.Visible = true
						local vector, onScreen = camera:WorldToScreenPoint(v.GuiPart.Position)
						plr.PlayerGui.MainGUI.EggFrame.Position = UDim2.new(0,vector.X - 100,0,vector.Y - 180)

						plr.PlayerGui.MainGUI.EggFrame.Egg.Value = v.Name
						print("Near An Egg")

					end
				
			else
=				plr.PlayerGui.MainGUI.EggFrame.Visible = false
				plr.PlayerGui.MainGUI.EggFrame.Egg.Value = ""
				
				
			end


		end
end)
1 Like

With … EggFrame.Egg.Value, did you mean … EggFrame.Egg.Text ?

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

local eggFrame = player.PlayerGui.MainGUI.EggFrame
local eggs = workspace:WaitForChild("Eggs"):GetChildren()

local db

RunService.Heartbeat:Connect(function()
	for i, v in pairs(eggs) do
		local magnitude = (v.Egg.Position - hrp.Position).Magnitude
		
		if (magnitude <= 8) then
			if (not eggFrame.Visible) then
				db = false
				eggFrame.Visible = true
				local vector, onScreen = camera:WorldToScreenPoint(v.GuiPart.Position)
				eggFrame.Position = UDim2.new(0, vector.X - 100, 0, vector.Y - 180)
				eggFrame.Egg.Text = v.Name
				print("Near the egg.")
			end
		else
			db = true
			eggFrame.Visible = false
			eggFrame.Egg.Text = ""
		end
	end
end)

Oh, no, Egg is a string value. I have another local script which detects if this string value changes then shows the information for whats pets are in the egg

Hmm, I can’t seem to figure out how I would add a different functionality to hiding the guis. Got any suggestions?

Apparently, it’s the loop problem. It’s quite obvious once you take a look at it again and understand what is going on. :smiley: Your for-loop performs well, but the magnitude only works for the last part, last element of the group. The magnitude is always of the element that comes last in order. To prevent that, we need to measure all distances, and compare the closest one with the limit. We use a for-loop to get magnitudes of each element (egg), and find the final, lowest magnitude. I used DistanceFromCharacter(), which is pretty much the same and calculates magnitude, but saves us from defining HumanoidRootPart.

If the following doesn’t work, it’s certainly because of either paths not matching (I’ve made some small changes) or some other unrelated error.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local eggFrame = player.PlayerGui.MainGUI.EggFrame
local eggs = workspace:WaitForChild("Eggs"):GetChildren()

local distance, nearest

while (true) do
	-- Reset the distance.
	distance = 10
	for i, v in pairs(eggs) do
		local eggDist = player:DistanceFromCharacter(v.Egg.Position)
		-- Find the closest egg in the folder.
		if (not distance or eggDist < distance) then
			distance = eggDist
			nearest = v
		end
	end

	if (distance <= 8) then
		eggFrame.Egg.Value = nearest.Name
		if (not eggFrame.Visible) then
			eggFrame.Visible = true
			local vector, onScreen = camera:WorldToScreenPoint(v.GuiPart.Position)
			eggFrame.Position = UDim2.new(0, vector.X - 100, 0, vector.Y - 180)
			print("Near the egg.")
		end
	else
		eggFrame.Visible = false
		eggFrame.Egg.Value = ""
	end
	wait(0.1)
end

Hi! This solution is better than before, but there are still some problems, I will attach a video to show the problem. First of all, when I go near the basic egg( the one on the left), it doesn’t show the gui but if i go to the blue egg from the basic egg it shows it. If I go straight to the blue egg it shows the gui.

robloxapp-20210225-2140321.wmv (2.9 MB)

It works perfectly fine for me, so can you please try again? Make sure eggs are built correctly. Any errors? I’ve updated the previous code with minor changes for the better and removed the RunService.Heartbeat, because previous repeat rate was unnecessary high.

Sorry, for replying late. It works now. Thank you so much. I will keep in mind this method for other uses.

1 Like