I am trying to make a local script that tweens all billboard GUIS when the player is in a certain range.
I chose to loop through all the billboard guis in the game every heartbeat. I’m a bit concerned about performance and I just wanted to make sure that the local script I made for this wouldn’t cause any issues performance-wise.
If there are any, please give feedback!
local RunService = game:GetService("RunService")
local maxDistance = 15
RunService.Heartbeat:Connect(function()
for i, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BillboardGui") then
local distance = (v.Adornee.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude
if distance >= maxDistance then
v.Enabled = false
--tween here
else
v.Enabled = true
--- tween here
end
end
end
end)```
this should work (didnt tested it so it might have bugs)
task.wait(1)
local guisGroup = script.Parent
local sizes = {}
local guis = {}
for i,gui in pairs(guisGroup:GetDescendants()) do
if gui:IsA("BillboardGui") then
table.insert(guis,gui)
table.insert(sizes,gui.Size)
end
end
local maxDistance = 15
local function popUp(gui,size)
gui:TweenSize(
UDim2.new(size.X, size.Width, size.Y, size.Height), --endSize
Enum.EasingDirection.Out, -- default
Enum.EasingStyle.Elastic, -- easingstyle
5, -- time
true -- I set it just incase there is another tween happening
)
end
local function hide(gui,size)
gui:TweenSize(
UDim2.new(0, 0, 0, 0), --endSize
Enum.EasingDirection.Out, -- default
Enum.EasingStyle.Elastic, -- easingstyle
5, -- time
true -- I set it just incase there is another tween happening
)
end
game["Run Service"].Heartbeat:Connect(function()
for i, gui in pairs(guis) do
local distance = (gui.Adornee.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude
if distance >= maxDistance and gui.Enabled == true then
gui.Enabled = false
hide(gui,sizes[i])
elseif distance < maxDistance and gui.Enabled == false then
gui.Enabled = true
popUp(gui,sizes[i])
end
end
end)
basically what it does is to store all the guis in a table as first, then everytime checks for every gui in the table if they’re too far or not
well, I’ve maked sure the script has better performance (instead of doing the loop trough every single descandant of the group every frame of the game, it just stores all guis in one table at the start of the game (this will make sure the script runs smoothly), then it checks if the gui has alredy been enabled or not, so it wont do the tween everytime).