I'm trying to put a gui's adornee on the closest part. The part is spawned when people click a button. I tried to use this script:

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:WaitForChild("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:WaitForChild("NumVal").Value > n and v:WaitForChild("NumVal").Value < 10 then
					rK.Enabled = true
					rK.Adornee = v
					n.Value = (v:WaitForChild("NumVal").Value)
				end
			else
				n = (v:WaitForChild("NumVal").Value)
			end
		end
	end
	wait(0.25)
end

but it says "attempt to index global 'n' (a number value)" Also, the person who spawned the part can only see the gui on that part.

Any help?

2 comments

0

line 22: n.Value?

0

yas, whats n.Value? n is a variable??