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!! ![]()
The code:
The things that I dont understand are:
-
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?
-
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)
