how to make a Damage Script in Roblox studio

-- Step 1: Define a function to handle damage
local function dealDamage(target, amount)
    -- Step 2: Check if the target exists and has a humanoid
    if target and target:IsA("Model") and target:FindFirstChildOfClass("Humanoid") then
        local humanoid = target:FindFirstChildOfClass("Humanoid")

        -- Step 3: Subtract the damage amount from the target's health
        humanoid.Health = humanoid.Health - amount

        -- Step 4: Check if the target's health is less than or equal to 0
        if humanoid.Health <= 0 then
            -- Step 5: If the target's health is 0 or below, handle the defeat (e.g., respawn or other actions)
            -- Replace the code below with your desired actions upon defeating the target
            print(target.Name .. " has been defeated!")
        end
    end
end

-- Step 6: Connect the function to an event that triggers damage (e.g., a button click, collision, etc.)
local targetObject = game.Workspace:WaitForChild("Target") -- Replace "Target" with the actual name of your target object
local damageAmount = 10 -- Replace 10 with your desired damage amount

-- Connect the function to an event (e.g., a button click)
targetObject.MouseClick:Connect(function()
    dealDamage(targetObject, damageAmount)
end)