How would I get the player who is paired with the magnitude to damage him?

I am trying to find the lowest magnitude to only damage 1 person in an area that I punched in, but I am having trouble on finding the player that has the lowest magnitude.

I found the lowest magnitude as I stored them in a table, but I need to find out who is paired with that magnitude so I can damage the player.

I can index all of the players but how would I check that they are paired to the lowest magnitude?

local magnitudeRange = {}
local lowestRange = 1000
	for i, v in ipairs(game.Workspace.Players:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:FindFirstChild("Torso") and v ~= char then 
			
			module.playerDistanceCheck = (char.HumanoidRootPart.CFrame * CFrame.new(0,0,-4).Position - v.HumanoidRootPart.Position).magnitude
					
			if module.fistCombo ~= 5 and module.playerDistanceCheck <= module.fistRange then
				magnitudeRange[v] = module.playerDistanceCheck
				print(v.Name.. " has " ..magnitudeRange[v])
				
				
				for player, magnitude in pairs(magnitudeRange) do
					if magnitude < lowestRange then
						lowestRange = magnitude -- I found the lowest magnitude, but how would I get the player who is paired to it?
						-- Damage player here etc.
					end
				end```
1 Like

the player would be v from the first for loop.

How would I refer to the player though? Because there’s more than 1 player.

Ah, to damage the lowest ranged player at the end, add a variable at the start and define it when the lowest player is found like so:

local magnitudeRange = {}
local lowestRange = 1000
local lowestplr
	for i, v in ipairs(game.Workspace.Players:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:FindFirstChild("Torso") and v ~= char then 
			
			module.playerDistanceCheck = (char.HumanoidRootPart.CFrame * CFrame.new(0,0,-4).Position - v.HumanoidRootPart.Position).magnitude
					
			if module.fistCombo ~= 5 and module.playerDistanceCheck <= module.fistRange then
				magnitudeRange[v] = module.playerDistanceCheck
				print(v.Name.. " has " ..magnitudeRange[v])
				
				
				for player, magnitude in pairs(magnitudeRange) do
					if magnitude < lowestRange then
						lowestRange = magnitude -- I found the lowest magnitude, but how would I get the player who is paired to it?
						lowestplr = v
						-- Damage player here etc.
					end
				end

Alright I’ll try and see if it works

Doesn’t work because it cycles through all of the players regardless if they are the lowest range or not. I need it so that it finds the specific player who is paired to the lowest magnitude so I can only damage him

Yes, which is why you will damage after the player loop.

The problem is that it will damage everyone in range of the fists instead of the closest player.

The issue here is that you’re looping through to find the lowest range, but doing damage while in that same loop, meaning, it’ll consistently hit multiple targets for as long as they’re closer than the previous range.

To solve this, let the loop run through to completion, and then take the closest player and deal damage to them, rather than dealing damage while the loop is running.

for player, magnitude in pairs(magnitudeRange) do
					if magnitude < lowestRange then --current range compared is closer than the previous closest range
						lowestRange = magnitude --set the new lowest range found so you can compare the other ranges
						lowestplr = v --as it's the most recent closest player, mark them down
					end
				end
if lowestplr then --make sure it exists first to prevent erroring
--deal damage here; this is the closest player found, and will not run again
end

It still damages both of the players because I need a way to find the exact player paired to the lowest magnitude, because v is all of the players in range but not exactly the closest one. Is there a way I can do it in the loop or would I have to find another way?

Typo on my behalf, change lowestplr = v to lowestplr = player.

I just tested the script, and it still damages both players. Apparently it damages both of them still. This is the script:

for i, v in ipairs(game.Workspace.Players:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:FindFirstChild("Torso") and v ~= char then 
			
			module.playerDistanceCheck = (char.HumanoidRootPart.CFrame * CFrame.new(0,0,-4).Position - v.HumanoidRootPart.Position).magnitude
					
			if module.fistCombo ~= 5 and module.playerDistanceCheck <= module.fistRange then
				magnitudeRange[v] = module.playerDistanceCheck
				print(v.Name.. " has " ..magnitudeRange[v])
				
				
				for player, magnitude in pairs(magnitudeRange) do
					if magnitude < lowestRange then
						lowestRange = magnitude
						lowestPlayer = player
					end
				end
				
				if lowestPlayer then
					print(lowestPlayer)
					lowestPlayer.Humanoid:TakeDamage(5)
				end

It prints two different lowestPlayers and damages both of them.

Solved, had to put the second loop outside of the first one.