Trying to make an attack hitbox through a tutorial video but I don't quite understand. Can somebody explain some parts of the code?

Hello, I am making an attack hitbox script via following a video but I dont understand some parts of the code. Any advice is appreciated!! :smiley:

The code:

The things that I dont understand are:

  1. Why write “if character:FindFirstChild(“Attacking”) then” before Attacking (the folder) has even existed yet since you defined the Attacking folder AFTER you wrote this IF statement? Cuz i thought LUA reads from up to down, right?

  2. Can you explain what “alreadyHit[targetHit] = true” is? Cuz i cannot understand how it stops repeated hits

Code for those who wants to paste it in Studio:
–Combat System
local debris = game:GetService(“Debris”)
local replicatedStorage = game:GetService(“ReplicatedStorage”)

local assets = replicatedStorage.Assets
local remotes = replicatedStorage.Remotes

remotes.Attack.OnServerEvent:Connect(function(player: Player, argumentData)
local character = player.Character
–Checking if player has character
if character then
local humanoid: Humanoid = character:FindFirstChild(“Humanoid”)
if humanoid then
if character:FindFirstChild(“Attacking”) then
return
end
–Creating Attacking Folder
local attacking = Instance.new(“Folder”)
attacking.Name = “Attacking”
attacking.Parent = character
–Creating Hitbox
local hitboxClone = assets.HitboxTemplate:Clone()
hitboxClone.Name = “Hitbox”

		--Welding Hitbox to HumanoidRootPart
		local weld = Instance.new("Weld")
		weld.Part0 = character.HumanoidRootPart
		weld.Part1 = hitboxClone
		weld.C0 = CFrame.new(0,0,-3)
		weld.Parent = workspace.Hitboxes
		hitboxClone.Parent = workspace.Hitboxes
		
		--Looking for Humanoid in Hitbox
		task.spawn(function()
			local params = OverlapParams.new()
			params.FilterType = Enum.RaycastFilterType.Exclude
			params.FilterDescendantsInstances = {character, workspace.Hitboxes}
			
			local alreadyHit = {}
			
			--Hitbox Loop
			while hitboxClone.Parent do
				--Checking for Humanoid inside Hitbox
				local partsCollected = workspace:GetPartsInPart(hitboxClone, params)
				for _, part in partsCollected do
					if part.Parent and part.Parent:FindFirstChild("Humanoid") then
						
						local targetHit = part.Parent
						if alreadyHit[targetHit] then
							return
						end
						alreadyHit[targetHit] = true
						local targetHumanoid: Humanoid = targetHit.Humanoid
						
						targetHumanoid:TakeDamage(5)
					end
				end
				print(partsCollected)
				task.wait()
			end
		end)
		
		debris:AddItem(hitboxClone, 0.35)
		debris:AddItem(attacking, 0.5)
		
		print(player.Name, "Attack")

	end
end

end)

That’s not a good implementation, honestly, and is not performant at all; it looks a lot like a prototype code, or the author of the tutorial does not know what he is really doing.
Kind of a unsafe code, I’d say as well.

I can’t really explain to you that if you don’t know much of luau

Basically, it does a pretty expensive check without quick pre-validation for rate limit, and it does spawn threads inside another thread (task.spawn), but this event already is a thread itself if that makes sense to you.
It also for no reason yields the thread, and it’s a very bad idea for task scheduler.
I suggest you learn Luau yourself and write code by yourself with this method:

https://create.roblox.com/docs/reference/engine/classes/WorldRoot#GetPartsInPart

or this
https://create.roblox.com/docs/reference/engine/classes/WorldRoot#GetPartBoundsInBox