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
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
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)