Why does this not work?

So I’m making a melee system.

I have a problem where when a player / npc is added, you can’t hit them.

Is this a problem with ROBLOX, or a problem with my code?

for _, v in pairs(game.Workspace:GetDescendants()) do
			if v:FindFirstChild("Humanoid") then
				if v:FindFirstChild("Head") then
					if v == tool.Parent then return end
					local magnitude = (v.Head.Position-tool.Parent.Head.Position).magnitude
					if magnitude < radius then
						local directionVector = (v.HumanoidRootPart.CFrame.p - tool.Parent.HumanoidRootPart.Position).unit
						local velocity = directionVector * 75
						if math.random(1,1) == 1 then
							--v.Ragdoll.Tripped.Value = true -- don't want fists to trip do u?
						end
						v.HumanoidRootPart.Velocity = velocity
						v.Humanoid:TakeDamage(dmg)
						critfunc(critchance,v)
						pickrandomsound(tool.Handle.Hits,tool.Handle)
						gibs(v)
					end
				end
			end
		end

Nevermind, I fixed it.

It was because of the “return end” near the start.

You aren’t using PlayerAdded to find out when a player or NPC joins the game.

Your script is going through EVERY item in the game looking for a Humanoid and Head, but it’s going to cause lag if you have thousands of Parts.

PlayerAdded fires when someone joins the game.

No, it’s in a tool and it doesn’t need to.

Yeah, but then it WOULDN’T work with npcs.

Then try limiting where the script is looking for the NPCs. If you put them into a Folder in the Workspace then it’d only only search game.Workspace.(Name of Folder):GetDescendants()

I was doing that, but I thought it was the problem to the code.

It wasn’t.