Help with detecting humanoids

I’m trying to make a script where it detects any character in a 20 (stud?) radius. So far it’s not detecting my dummy and i have no idea why

local tool = script.Parent

local debounce = false


tool.Shot.OnServerEvent:Connect(function(plr, mousePos)
	if not debounce then
		debounce = true
		local function FindNearestTargets()
			local targets = {}
			
			local workspaceStuff = workspace:GetChildren()
			
			for i, thing in workspaceStuff do
				local hum = thing:FindFirstChild("Humanoid")
				if hum then
					local dis = (plr.Character.HumanoidRootPart.Position - thing.HumanoidRootPart.Position).Magnitude
					if dis <= 20 then
						table.insert(targets, thing)
					end
				end
			end
		end

		local target = FindNearestTargets()
		if target then
			print(target.Name)
			target.Humanoid:TakeDamage(30)
		end
		task.wait(1)
		debounce = false
	end
end)
1 Like

From your FindNearestTargets() functions you aren’t returning anything (not even the nearest humanoid). (At least of what I think so).

1 Like

target.Name will always be nil since there is no return statement in the function. It looks like you want to return a Boolean value. I would add this:

				if hum then
					local dis = (plr.Character.HumanoidRootPart.Position - thing.HumanoidRootPart.Position).Magnitude
					if dis <= 20 then
						table.insert(targets, thing)
					end
                   return true
                else 
                   return false
				end

In this line

local target = FindNearestTargets()
		if target then
			print(target.Name)
			target.Humanoid:TakeDamage(30)
		end

you are expecting the function to return a model (character). Yet FindNearestTargets() doesn’t actually return a character, let alone anything.