How to detect if humanoid is out or in radius

I did some research and i managed to do a quite decent script about what title said but it only works detecting once, even though it’s a loop

The issue is quite simple, the script only detects the humanoid in or out range of radius once, the same if get closer to the humanoid or get further away

--this is a server script
local closest
local maxrange = 20
	for _, humanoids in pairs(game.Workspace:GetDescendants()) do
		if humanoids:IsA("Humanoid") and humanoids ~= character:FindFirstChild("Humanoid") then
			local target = humanoids:FindFirstAncestorOfClass("Model")
			if target and target ~= character then
				if target.PrimaryPart or target:FindFirstChild("Humanoid") then
					print(target.Name)
					local distance = (humanoidrootpart.Position - target:FindFirstChild("HumanoidRootPart").Position).magnitude
					if distance <= maxrange then
						closest = target
						print(target.Name.." is in radius")
					else
						print(target.Name.." no longer in radius")
						closest = nil
					end
				end
			end
		end
	end

appreciate any help of yours.

You could put it in a while or a repeat loop, so that it checks, say, every couple seconds.

For example:

repeat
-- your code
until false

I did as you said but it’s still the same

Let me try your script in my own game.

local closest
local maxrange = 20
for i = 1, math.huge do -- Loop outside of detection loop.
task.wait(0.5)
	for _, humanoids in pairs(game.Workspace:GetDescendants()) do
		if humanoids:IsA("Humanoid") and humanoids ~= character:FindFirstChild("Humanoid") then
			local target = humanoids:FindFirstAncestorOfClass("Model")
			if target and target ~= character then
				if target.PrimaryPart or target:FindFirstChild("Humanoid") then
					print(target.Name)
					local distance = (humanoidrootpart.Position - target:FindFirstChild("HumanoidRootPart").Position).magnitude
					if distance <= maxrange then
						closest = target
						print(target.Name.." is in radius")
					else
						print(target.Name.." no longer in radius")
						closest = nil
					end
				end
			end
		end
	end
end

What are you trying to achieve? Are you trying to compare 2 players’ positions and see if they are close enough?

I think the author is trying to see which player is closest to a point.

I want to find nearest humanoid to player of both the player and npc humanoid

You could use run service:

game:GetService("RunService").Stepped:Connect(function()
	--this is a server script
	local closest
	local maxrange = 20
	for _, humanoids in pairs(game.Workspace:GetDescendants()) do
		if humanoids:IsA("Humanoid") and humanoids ~= character:FindFirstChild("Humanoid") then
			local target = humanoids:FindFirstAncestorOfClass("Model")
			if target and target ~= character then
				if target.PrimaryPart or target:FindFirstChild("Humanoid") then
					print(target.Name)
					local distance = (humanoidrootpart.Position - target:FindFirstChild("HumanoidRootPart").Position).magnitude
					if distance <= maxrange then
						closest = target
						print(target.Name.." is in radius")
					else
						print(target.Name.." no longer in radius")
						closest = nil
					end
				end
			end
		end
	end
end)

This script does detecting in and out range of radius but why is it also preventing the rest of my script from working

Include the rest of the script.
The section keeps repeating so your other half of the script doesn’t have time to be active.

But if i include the rest of the script into the loop it would make a mess, i can’t separate the script neither, the script works on a FireEvent

Could you show me your script? I could make a fix.

This script fixed my issue, thanks for all of your help

2 Likes

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