Loop magnitude doesn't find new players

1. What do I want to achieve?

I made a combat system using Magnitude, the player must press F to fire the pvp remote event, detect players nearby and damage them.

2. What is the issue?

The script:

pvp.Damage.OnServerEvent:Connect(function(player: Player)
	if table.find(damage_cooldown, player) or player.BatMode.Value then return end

	table.insert(damage_cooldown, player)

	task.delay(claw_cooldown, function()
		local foundPlr = table.find(damage_cooldown, player)
		if foundPlr then
			table.remove(damage_cooldown, foundPlr)
		end
	end)

	local char = player.Character
	local humanoid = char:FindFirstChild("Humanoid")
	
	if not humanoid then return end
	if humanoid.Health <= 0 then return end

	playSfx:FireAllClients(player.Character, "Slash")
	
	for _, vampire in players:GetPlayers() do
		print(vampire.Name .. " is in players list.")
		local vampChar = vampire.Character

		local distance = (char.PrimaryPart.Position - vampChar.HumanoidRootPart.Position).Magnitude
		if distance < 8 then
			if vampChar.Humanoid.Health <= 0 or vampire == plr then return end
			print(vampire.Name .. " is alive.")
			PlrDamage(plr, vampire)
		end
	end
end)

The line issue:

for _, vampire in players:GetPlayers() do
		print(vampire.Name .. " is in players list.")
		local vampChar = vampire.Character

		local distance = (char.PrimaryPart.Position - vampChar.HumanoidRootPart.Position).Magnitude
		if distance < 8 then
			if vampChar.Humanoid.Health <= 0 or vampire == plr then return end
			print(vampire.Name .. " is alive.")
			PlrDamage(plr, vampire)
		end
	end

For some reason, when a player joined the server, that player can damage the ones that have been in the server before the new one, however, the other players aren’t able to damage the new player.

3. What solutions have I tried so far?

  • I added prints to know which line starts breaking the detection, the print(vampire.Name .. " is in players list.") shows in the output, but all lines below local vampChar = vampire.Character do not anymore.
  • I even tried to add a pairs or ipairs in the for _, vampire in players:GetPlayers() do but it’s still the same.

No errors in the output, just the name of the player in the players list. What am I doing wrong here?

Maybe try checking your if statements?

I also notice that you are using return which could be ending the whole for loop, causing the new player to never be detected. I would put the print() and the PlrDamage() in an if statement that said vampChar.Humanoid.Health > 0 and vampire ~= plr.

Where do you define what players is? Is it referring to the Players service or do you have your own array of players?

If I had to guess, it would be that you create an array of the existing players when the user joins the game, but do not update it for every new user that joins later on.

Well it worked but somehow when I put it in a separated function, it stops working.

It is referring to the players service.

Like:

local function Attack(plr: Player)
local char = plr.Character
for _, vampire in players:GetPlayers() do
		print(vampire.Name .. " is in players list.")
		local vampChar = vampire.Character
		print(vampire.Name .. " has a character!")

		local distance = (char.PrimaryPart.Position - vampChar.HumanoidRootPart.Position).Magnitude
		if distance < 8 then
			if vampChar.Humanoid.Health > 0 and vampire ~= player then
				print(vampire.Name .. " is alive.")
				PlrDamage(player, vampire)
			end
		end
	end
	
	for _, npc in workspace.NPCs:GetChildren() do
		local humanoid = npc:FindFirstChild("Humanoid")

		local distance = (char.PrimaryPart.Position - npc.HumanoidRootPart.Position).Magnitude
		if distance < 8 then
			if humanoid.Health > 0 then
				Damage(player, npc)
			end
		end
	end
end

I will figure this new error out myself later, but for now it is working. Thanks

The solution seems to be found, but just for future reference, using return in a statement will stop it.

There is a way around this though. Instead of using ‘return’, you can use ‘continue’

Example:

if vampChar.Humanoid.Health <= 0 or vampire == plr then continue end