Problem checking distance from player to npc

I have a script that when a player gets close to the npc it will enable a script and when the player is far it will disable a script but, it keeps on saying that the player is far from it.
server script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
		local NPC = game.Workspace:WaitForChild("Tank")
		local Distance = (NPC:WaitForChild("HumanoidRootPart").Position - Character:WaitForChild("HumanoidRootPart").Position).Magnitude
		
		while true do
			wait(1)
			if Distance <= 30 then
				NPC.Script.Enabled = true
				print("close")
			else
				NPC.Script.Enabled = false
				print("Far")
			end
		end
	end)
end)

robloxapp-20230804-1637457.wmv (1.5 MB)

Screenshot 2023-08-04 163823

put the Distance variable inside of the while true loop so it updates

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.