Opinion on script

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.

1 Like

What? You don’t need a script to display the nearest gui. Just use the properties.

I mean if it works it works. The potential performance issues about descendant added I’d only worry about if you are adding a ton of things and it’s actually affecting your game. Generally speaking how I would manage this however would be something that just alerts the player when a new billboard is added, or put them all in a folder and set their adornee property instead so all of them will end up in the same place and I wouldn’t have to check for updates. The folder method is recommended imo, but I spawn all my stuff like that from my scripts usually, so if you don’t do that it might make organization trickier.

The max distance property would only work to show them when they are in range, not switch between them when multiple are in range. Unless I’m totally neglecting a property I don’t think this can be done without a script.

Though depending on use, a proximity prompt might work.

1 Like

Okay then, apologies I think I’ve missed something from this article.

Or tag them with collection service. That would work too. Probably best in fact. I kind of always forget this exists until after I write something to do it’s job.

use variables to shorten: local a = WhateverObjectThisIsYouAreReferringToThatIsVeryAnnoyinglyBigAndIrritatingToMyEyesWellItShouldNotBeLikeThatAtAll