Why wont this script run?

there are no errors in my output and i looked over every line of code and i cant seem to find the problem with the code. Its ine a server script and its parented to a dummy

local NPC = script.Parent
local Players = game.Players
local PFS = game:GetService("PathfindingService")

local npcHum = NPC.Humanoid
local npcHRP = NPC.HumanoidRootPart
local closestDistance = math.huge

for i, player in pairs(Players:GetPlayers()) do
	print(player,"is player", i)
	local char = player.Character
	if char then
		local HRP = char.HumanoidRootPart
		if HRP then
			local distance1 = (npcHRP.Position - HRP.Position).Magnitude
			if distance1 < closestDistance then
				print(distance1)
				local closestHRP = HRP
			end
		end
	end
end

Heres the code ^

Help is much appreciated. Thank you for your time

1 Like

It might be running before any players join. To fix this, use the PlayerAdded event.

1 Like