Player Distance not working (Fixed)

Hello there,

So I am having trouble with my distance script everytime I make it where if the billboard is not enabled I set it to textLabel.Text = "" it breaks the script but when I remove that part it works idk why it’s doing it but I can’t seem to fix it.

Working script

local Destinations = {}
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local textLabel = script.Parent

local function updateDistance(part)
	if part.BillboardGui.Enabled then
		local distance = (player.Character.HumanoidRootPart.Position - part.Position).Magnitude
		textLabel.Text = distance > 1000 and string.format("%.1f mi", distance / 1000) or string.format("%.1f ft", distance)
	end
end

for _, folder in ipairs(game.Workspace:GetChildren()) do
	if folder.Name:match("Destinations$") then
		for _, destination in ipairs(folder:GetChildren()) do
			table.insert(Destinations, destination)
		end
	end
end

while true do
	for _, destination in ipairs(Destinations) do
		updateDistance(destination)
	end

	wait(0.01)
end

Broken script

local Destinations = {}
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local textLabel = script.Parent

local function updateDistance(part)
	if part.BillboardGui.Enabled then
		local distance = (player.Character.HumanoidRootPart.Position - part.Position).Magnitude
		textLabel.Text = distance > 1000 and string.format("%.1f mi", distance / 1000) or string.format("%.1f ft", distance)
else
-- This is where it breaks the script! 
textLabel.Text = ""
	end
end

for _, folder in ipairs(game.Workspace:GetChildren()) do
	if folder.Name:match("Destinations$") then
		for _, destination in ipairs(folder:GetChildren()) do
			table.insert(Destinations, destination)
		end
	end
end

while true do
	for _, destination in ipairs(Destinations) do
		updateDistance(destination)
	end

	wait(0.01)
end

If someone could help me what the problem is that would be really helpful :slight_smile:

@Sim_Q1

2 Likes

What error does it return? Does it just run and do nothing?

1 Like

Yeah all it does when i add textLabel.text = "" is nothing happens when you enable the billboard and there no error in the output. I have checked it over about x30 now.

1 Like

Is there any way I can make it set the text to nil?

1 Like

textLabel.text = “”

Is correct.

ok but idk why when I add that in the script it breaks it and nothing comes up. I just add pcall(function) and nothing was in the output.

1 Like

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