I tried making an NPC that would follow the closest player in the game. Here's what I did.
(This script is parented to the NPC's Humanoid)
while true do wait()
local closestPlayer
local distance
local plrs = game.Players:GetPlayers()
for i = 1, #plrs do
local currentDistance = plrs[i]:DistanceFromCharacter(script.Parent.Parent.Head.Position)
if distance == nil then
closestPlayer = plrs[i]
distance = currentDistance
elseif currentDistance > distance then
closestPlayer = plrs[i]
distance = currentDistance
end
end
print(closestPlayer..", "..distance)
wait(.5)
end
The problem is that "closestPlayer" is a "userdata value"...?
I'm not sure what this could mean, but please show me what's going wrong here; I would love to have this problem fixed ASAP.

2 comments
Assuming my answer helped and was correct in fixing the problem, you wouldnt mind accepting it would you? X)
XD Sorry, I've completely forgot multiple times