Hitbox creation function

Hi everyone! :smile:
I hope you’re having an amazing day. I have a question on how I could better this code, its a hitbox function that basically creates a hitbox with region3s based on the size of the parameters passed through, with other parameters (that aren’t important, other than delay_time, which basically tells the runservice.Stepped() to stop updating after the value if it isnt nil).

I’m not satisfied however with how big of a block of code this is. How could I shorten it, and what am I doing that would be considered malpractice? I’ve considered a lot but to no avail does it really fix it, rather it breaks my code, but right now this is a rough draft.

function Moves_Module:Hitbox(Character, Hit_Table, Delay_Time, Damage, Hitbox_Update, Hitbox_Size, Impact_Sound, End_Time)
    local Can_Land_Hit = 				true;
    local Player = 						game.Players:GetPlayerFromCharacter(Character);
    local Settings_Folder = 			Player:FindFirstChild("Settings");
    local Acting =                      Settings_Folder.Acting;
    local Can_Attack = 					Settings_Folder.Can_Attack;
    local Ability_Enabled = 		    Settings_Folder.Ability_Enabled;
    if End_Time then
        delay(End_Time, function()
            Can_Land_Hit = false;
        end)
    end
    Hitbox_Update = RunService.Stepped:Connect(function()
        if not(Acting.Value) and (Can_Attack.Value) or not(Can_Land_Hit) then Hitbox_Update:Disconnect() return end
        local Position = (Character.HumanoidRootPart.CFrame + Character.HumanoidRootPart.CFrame.lookVector).Position;
        local Size = Hitbox_Size;
        local Hitbox = Region3.new(Position-(Size/2), Position+(Size/2));
        local PartsInRegion = game.Workspace:FindPartsInRegion3WithIgnoreList(Hitbox, Character:GetDescendants());

        for __, part in pairs(PartsInRegion) do
            local Hitting_Character = part.Parent;
            if (part.Parent:FindFirstChild("Humanoid")) and not(table.find(Hit_Table, Hitting_Character)) then
                local Hitting_Humanoid = Hitting_Character:FindFirstChild("Humanoid");

                table_insert(Hit_Table, Hitting_Character);

                if (Hit_Table) and (Delay_Time ~= nil) then
                    delay(Delay_Time, function()
                        table.remove(Hit_Table, table.find(Hit_Table, Hitting_Character));
                    end)
                end

                local Impact_Sound_Clone = Impact_Sound:Clone();
                Impact_Sound_Clone.Parent = Character.PrimaryPart;
                Impact_Sound_Clone.PlaybackSpeed = math.random(7,12)/10;
                Impact_Sound_Clone:Play();
                Debris:AddItem(Impact_Sound_Clone, Impact_Sound_Clone.TimeLength);
            end
        end
    end)
end

I know it’s a bit messy, please bare with me when I say I know :sweat_smile:… I hope its not too hard to read either!

PyroIusite

3 Likes

I reccomend you use raycast hitbox or Fast Cast instead I’ve heard that region 3 can be laggy at times.

1 Like

ok, i’ll take a look at it, i wanted to create my own function basically, so i’ll look and maybe try to create my own. thanks!