Here I have a localscript that handles displaying only the nearest BillboardGuis.
local player = game.Players.LocalPlayer
local character = script.Parent.Parent
local root = character.HumanoidRootPart
local KeyList = {}
for i, v in pairs(workspace:GetDescendants()) do
if v:IsA("BillboardGui") and v.Name == "Button" then
table.insert(KeyList, #KeyList+1, v)
end
end
workspace.DescendantAdded:Connect(function(obj)
if obj:IsA("BillboardGui") and obj.Name == "Button" then
table.insert(KeyList, #KeyList+1, obj)
end
end)
local bill = Instance.new("BillboardGui")
local oldNearest = bill
while true do
local KeyDistanceList = {}
for i, v in pairs(KeyList) do
table.insert(KeyDistanceList, i, (root.Position - v.Parent.Position).Magnitude)
end
local min = math.min(table.unpack(KeyDistanceList))
local nearest = KeyList[table.find(KeyDistanceList, min)]
nearest.Enabled = true
if oldNearest ~= nearest then
oldNearest.Enabled = false
end
oldNearest = nearest
wait(.2)
end
Now, this script works, but is there anything wrong that could affect FPS with using the following code:
for i, v in pairs(workspace:GetDescendants()) do
if v:IsA("BillboardGui") and v.Name == "Button" then
table.insert(KeyList, #KeyList+1, v)
end
end
workspace.DescendantAdded:Connect(function(obj)
if obj:IsA("BillboardGui") and obj.Name == "Button" then
table.insert(KeyList, #KeyList+1, obj)
end
end)
It feels like there would be some performance issues when you use workspace.DescendantAdded and then an if statement inside it. Maybe I’m just paranoid.
Maybe there is a simpler way to do this. Just looking to improve the script.