I'm trying to make a gui's adornee on a part that is spawned by someone when they click a button. Also, the person who spawned the part is the only one who can see the gui on that part. The gui will only pop up if the part is the closest to the player that spawned it and is ten studs away.
I tried doing this:
local plrName = script.Parent.Name
local rK = game.Players:GetPlayerFromCharacter(script.Parent).PlayerGui:WaitForChild("RKeyGen")
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 pAvg = (pPos.X + pPos.Y + pPos.Z)/3
local lAvg = (lPos.X + lPos.Y + lPos.Z)/3
local sAway = math.abs(pAvg - lAvg)
v.NumVal.Value = sAway
end
end
n = 0
for i, v in pairs(game.Workspace.Bricks:GetChildren())do
if v.Name == plrName then
if n ~= 0 then
if v.NumVal.Value > n and v.NumVal.Value < 10 then
rK.Enabled = true
rK.Adornee = v
n = v.NumVal.Value
end
else
n = v.NumVal.Value
end
end
end
wait(0.25)
end
But, the gui just pops up even when theres other parts closest to me and it stays on the same part even when i am like, 30 studs away from it.
Any help?

1 comment
If you're doing this in a local script (Which you should), you should use game.Players.LocalPlayer for line 02.