I am trying to use a script where a billboard gui pops up on the closest part to the player that the player spawned using a button. I tried using this script:
local plrName = script.Parent.Name
local rK = game.Players:GetPlayerFromCharacter(script.Parent).PlayerGui:WaitForChild("RKeyGen")
n = nil
while true do
for i, v in pairs(game.Workspace.Bricks:GetChildren())do
if v.Name == plrName then
local pPos = v.Position
local lPos = script.Parent:FindFirstChild("HumanoidRootPart").Position
local sAway = (pPos - lPos).magnitude
if sAway ~= nil then
v:FindFirstChild("NumVal").Value = tonumber(sAway)
print(sAway)
if n ~= nil then
if v:FindFirstChild("NumVal").Value > n then
rK.Enabled = true
rK.Adornee = v
else
n = v:FindFirstChild("NumVal").Value
end
else
n = v:FindFirstChild("NumVal").Value
end
end
end
end
wait(0.15)
end
It works sometimes, but then an error occurs randomly and says: "Workspace.snaks308070.Script:12: attempt to index a nil value"
Any help?
