Getting Magnitude

I’m trying to make a detection script for when a player gets closer to a part or model, but it’s not printing when i get closer. Any help?

The Code:

local Boss = workspace:WaitForChild("DemonDarkBoss")
local PointOne = workspace:WaitForChild("PointOne")
local TweenService = game:GetService("TweenService")
local Health = 100

local players = game.Players:GetPlayers()
for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		--get players current distance from parts/models center part
		local distance = (PointOne.Position - target.HumanoidRootPart.Position).Magnitude 

		--specify a max distance
		local radius = 100

		--check to see if player is within radius
		if distance < radius then
			print("Close")
		else
			print("Not Close")
		end
	end
end
2 Likes

Place your script in a loop . .

How? And what type of loop? I tried but didn’t work

should be a while loop, you need a loop because your only checking the player distance one time, you need to check it repeatedly; just simply wrap the for loop with a while loop

It’s still not working, I don’t know why

Remember that code only runs once, so you should either put it in a loop or connect it to an event that fires multiple times.

You can use while loops, but make sure you add a task.wait(). You could also use RunService.Heartbeat too. It should look a bit like this:

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
   local players = game.Players:GetPlayers()
   for index, player in pairs(players) do
	   if player.Character then
		   local target = player.Character
		   --get players current distance from parts/models center part
		   local distance = (PointOne.Position - target.HumanoidRootPart.Position).Magnitude 

		   --specify a max distance
		   local radius = 100

		   --check to see if player is within radius
		   if distance < radius then
			   print("Close")
		   else
			   print("Not Close")
		   end
	   end
   end
end)
5 Likes

Now I understand, my loop was incorrectly pasted in the code, making nothing work. Thanks for all the help

local Boss = workspace:WaitForChild("DemonDarkBoss")
local PointOne = workspace:WaitForChild("PointOne")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

while task.wait() do
	for index, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local target = player.Character
			--get players current distance from parts/models center part
			local distance = (PointOne.Position - target.HumanoidRootPart.Position).Magnitude 
			--specify a max distance
			local radius = 100
			--check to see if player is within radius
			if distance < radius then
				print("Close")
			else
				print("Not Close")
			end
		end
	end
end

The code would only ever run once the first time the script is executed, I’ve enclosed the bit you want to run repetitively inside a loop. I would advise you don’t execute this forever though, and use a property changed signal to trigger when the block of code should be executed.