Closest part, Error?

I’m trying to get BillboardGui’s that are parented to the closest door, to become visible when I’m within the maximum distance. But I keep getting this error for some reason and I’m stuck trying to figure out how to solve this.

--// OUTPUT ERROR
03:37:53.238 - RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: ReplicatedStorage.Assets.Modules.GuiService:20: bad argument #2 to '?' (Vector3 expected, got nil)
03:37:53.252 - ReplicatedStorage.Assets.Modules.GuiService:20: bad argument #2 to '?' (Vector3 expected, got nil)
--// MODULE (SERVER & CLIENT).
--// Variables
local GuiService = {}
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Doors = game.Workspace.World.Doors:GetDescendants()
local MaxDistance = 15

function GuiService:EnableNearestBillboardGui(Part)
	for index, descendant in pairs(Part) do
		local Distance = (Root.Position - Part.Position).magnitude
		if Distance < MaxDistance then
			local Billboard = Part:FindFirstChildOfClass("BillboardGui")
			if Billboard then 
				Billboard.Enabled = true
			else
				Billboard.Disabled = true
			end
		end
	end
end

function GuiService:Initialize()
	RunService:BindToRenderStep("BillboardGuiBinding", 1, function()
		GuiService:EnableNearestBillboardGui(Doors)
	end)
end

return GuiService
--// CLIENT
local ReplicatedStorage = game.ReplicatedStorage.Assets.Modules
local GuiService = require(ReplicatedStorage.GuiService)

wait(10)
GuiService:Initialize()

Part is an array, as you have passed Doors in the bind to render step, where Doors points to a table returned by :GetDescendants.

You meant to write descendant.Position.

1 Like

:+1: Nice, that was the problem.

The issue now is that it doesn’t want to listen to the “else” statement. They billboard keeps staying enabled after the distance has been exceeded.

That issue is because your else statement is attached to if Billboard then, but it should be attached to if Distance < MaxDistance then So doing something like this should fix that problem:



function GuiService:EnableNearestBillboardGui(Part)
	for index, descendant in pairs(Part) do
local Distance = (Root.Position - Part.Position).magnitude
local Billboard = Part:FindFirstChildOfClass("BillboardGui")
  if Distance < MaxDistance and Billboard then
		Billboard.Enabled = true
elseif Billboard then 
		  Billboard.Disabled = true
		end
	end
    end
end
2 Likes

Thanks for your help, greatly appreciated :+1: