Can anyone explain this script works

Hello fellow developers, someone recently sent me a few addons of code for my script but I’ll just put the whole thing, this is the script:

script.Parent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char.Humanoid
	local Smallest, Enemy = math.huge, nil
	local root = char.HumanoidRootPart
	for i,v in pairs(workspace:GetChildren()) do
		if v:FindFirstChild("HumanoidRootPart") and v ~= char then
			local dis = (root.Position - v.HumanoidRootPart.Position).Magnitude
			if dis < 60 and dis < Smallest then
				Smallest = dis
				Enemy = v
			end	
		end
	end
	print(Enemy)
end)

I’m not sure how it works and I think it could be very helpful information (the script gets the closest player btw) for the future, thank you!

Im pretty sure it gets the nearest enemy from the player character.
local char = plr.Character --character of player
local hum = char.Humanoid --humanoid of player
local Smallest, Enemy = math.huge, nil --setting the value so that the first enemy can be assigned
local root = char.HumanoidRootPart–hrp of player
for i,v in pairs(workspace:GetChildren()) do–looping everyitems in workspace
if v:FindFirstChild(“HumanoidRootPart”) and v ~= char then–check if its a npc or player
local dis = (root.Position - v.HumanoidRootPart.Position).Magnitude–get a distance
if dis < 60 and dis < Smallest then --check the distance condition
Smallest = dis --this is a smallest distance of the enemy (might not be final result)
Enemy = v --this is a nearest enemy(might not be final result)
end
end
end
print(Enemy) --prints out a enemy

There’s nothing wrong with using descriptive variable names.
Optimize for readability.

script.Parent.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character.Humanoid
	local humanoidRootPart = character.HumanoidRootPart
	local smallest, enemy = math.huge, nil
	
	for _, otherCharacter in pairs(workspace:GetChildren()) do
		if otherCharacter:FindFirstChild("HumanoidRootPart") and otherCharacter ~= character then
			local distance = (humanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude
			
			if distance < 60 and distance < smallest then
				smallest = distance
				enemy = otherCharacter
			end	
		end
	end
end)

The only somewhat confusing thing here is the distance calculation.
See Magnitude.

1 Like

I did understand everything except for the part where its like

Smallest = dis
Enemy = v

because it would do that for every single npc/player but for some reason it only gets one in the end and specifically the closest one and I’m not sure how that would work.

Those declarations are hoisted before the for loop. Every iteration of the for loop it’s comparing the distance with Smallest. If distance is smaller it assigns distance to Smallest; making Smallest now be that distance.

It’s doing the same overwrite assignment to Enemy. The print(Enemy) happens after the loop is completed, so what it’s printing is the last thing assigned to Enemy.

Ah, now I see thank you for your help!

1 Like