Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how do i make a part that if you give damage to them it will spawn parts?

Asked by 1 year ago

i tried to do that but i didnt worked it looks so simple but i keep over thinking what i wanna do is if you do damage to a part (like some weapon) it will spawn some parts to make it fell like it broke. i'm also trying to figure it out how to make a part take damage on fall.

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

This code can be used to break a part into multiple other parts, for if a part loses health or is destroyed:

function breakPart(part)
    if part ~= nil then --makes sure that the part exists
        if part.ClassName == "BasePart" or part.ClassName == "Part" or part.ClassName == "MeshPart" or part.ClassName == "Union" then --checks if the part is actually a part
            local split = 2 --split the original part into this many smaller parts on each side (for example, it might split it by 2 lengthwise, widthwise, and heightwise)

            local original_size = part.Size
            local original_position = part.Position
            local original_CFrame = part.CFrame

            local original_color = part.Color
            local original_material = part.Material
            part:Destroy() --removes the original part

            local start_x = original_position.X - original_size.X/2
            local start_z = original_position.Z - original_size.Z/2
            local start_y = original_position.Y - original_size.Y/2

            local holder_model = Instance.new("Model")

            while start_x < original_position.X + original_size.X/2 do
                while start_z < original_position.Z + original_size.X/2 do
                    while start_y < original_position.Y + original_size.X/2 do
                        local smaller_part = Instance.new("Part")
                        smaller_part.Parent = holder_model
                        --sets size and position
                        smaller_part.Size = Vector3.new(original_size.X/split, original_size.Y/split, original_size.Z/split)
                        smaller_part.Position = Vector3.new(start_x, start_y, start_z)
                        --sets the color and material
                        smaller_part.Color = original_color
                        smaller_part.Material = original_material
                        start_y += original_size.X/split
                    end
                    start_y = original_position.Y - original_size.Y/2
                    start_z += original_size.Z/split
                end
                start_z = original_position.Z - original_size.Z/2
                start_x += original_size.X/split
            end
            holder_model:PivotTo(original_CFrame)
            holder_model.Parent = workspace

            --waits for a set time before destroying the parts
            --remove the next two lines if you don't want it to destroy them
            wait(10)
            holder_model:Destroy()
        end
    end
end

Then, you can implement it in say, weapons, or when a part falls.

An example of how you might implement it in a weapon (Script directly inside a tool):

local debounce = false
local cooldown = 1


function breakPart(part)
    if part ~= nil then --makes sure that the part exists
        if part.ClassName == "BasePart" or part.ClassName == "Part" or part.ClassName == "MeshPart" or part.ClassName == "Union" then --checks if the part is actually a part
            local split = 2 --split the original part into this many smaller parts on each side (for example, it might split it by 2 lengthwise, widthwise, and heightwise)

            local original_size = part.Size
            local original_position = part.Position
            local original_CFrame = part.CFrame

            local original_color = part.Color
            local original_material = part.Material
            part:Destroy() --removes the original part

            local start_x = original_position.X - original_size.X/2
            local start_z = original_position.Z - original_size.Z/2
            local start_y = original_position.Y - original_size.Y/2

            local holder_model = Instance.new("Model")

            while start_x < original_position.X + original_size.X/2 do
                while start_z < original_position.Z + original_size.X/2 do
                    while start_y < original_position.Y + original_size.X/2 do
                        local smaller_part = Instance.new("Part")
                        smaller_part.Parent = holder_model
                        --sets size and position
                        smaller_part.Size = Vector3.new(original_size.X/split, original_size.Y/split, original_size.Z/split)
                        smaller_part.Position = Vector3.new(start_x, start_y, start_z)
                        --sets the color and material
                        smaller_part.Color = original_color
                        smaller_part.Material = original_material
                        start_y += original_size.X/split
                    end
                    start_y = original_position.Y - original_size.Y/2
                    start_z += original_size.Z/split
                end
                start_z = original_position.Z - original_size.Z/2
                start_x += original_size.X/split
            end
            holder_model:PivotTo(original_CFrame)
            holder_model.Parent = workspace

            --waits for a set time before destroying the parts
            --remove the next two lines if you don't want it to destroy them
            wait(10)
            holder_model:Destroy()
        end
    end
end


script.Parent.Handle.Touched:Connect(function(part)
    if not debounce then
        debounce = true
        local valid = true
        --check if the part is inside the character
        if part.Parent ~= workspace then
            if part.Parent.Parent == script.Parent.Parent
                valid = false
            end
        end
        if part.Parent == script.Parent.Parent then
            valid = false
        end
        -if the part is not inside the character:
        if valid then
            --Create a coroutine so that the break part function can run in the background
            local taskCoro = coroutine.create(breakPart)
            coroutine.resume(taskCoro, part)

            wait(cooldown)
            debounce = false
        end
    end
end)

You can also implement the break part function in other places that you might want to break a part into multiple parts. All you need to do is do breakPart(part that you want to break). To change how many smaller parts it splits into, you can change the split value on line 4 of the function. You can also change how long the split parts stay in the world at the end of the function.

0
it didnt worked do i have to put the script in the workspace? do i have to add some event? ithallo_brasil345 9 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Well, you can use the event .HealthChanged in a humanoid to detect when they lose or gain health. A code example would be:

humanoid.HealthChanged:Connect(function(hp)
    print(hp)
    -- the output will print the remaining hp
end)

With this information in mind, we can detect when a player loses health by using this code (the humanoid variable does not exist, so replace that with your own humanoid variable)

local currentHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(hp)
    if currentHealth > hp then
        print("player lost health!")
        local p = Instance.new("Part",workspace)
        p.Size = Vector3.new(.25,.1,.25)
        p.CFrame = humanoid.Parent.HumanoidRootPart.CFrame
        p.BrickColor = BrickColor.new("Crimson")
        local p2 = p:Clone()
        p2.Parent = workspace
        local p3 = p:Clone()
        p3.Parent = workspace
        game.Debris:AddItem(p1,1)
        game.Debris:AddItem(p2,1)
        game.Debris:AddItem(p3,1)
    end
end)

Please note none of this was tested. It was made in a rush. If you have any issues, feel free to comment.

For more information view this link.

Answer this question