Earthquake system need help

Hey im trying to build a earthquake shaking system that will break anyparts welds.

But only if the part has a Breakable boolvalue i currently has this but i really need help as nothing seems to be working and region3 is really buggy ive tried theres also been problems with lagging.

and somehow it doesnt work i saw a similar system in a game called Breakwater Blitz 2008
also maybe like similar game like natural disaster survival

i really want help as i dont know how i would detect a part touching another part then delete its welds when its touching the main earthquake part

-- Main part where this script is located
local mainPart = script.Parent

-- Earthquake parameters
local shakeDuration = 5                         -- Duration of the earthquake (in seconds)
local shakeInterval = 0.5                       -- Interval between shakes (to reduce update frequency)
local shakeMagnitude = Vector3.new(2, 0, 2)     -- Mild shaking strength for linear velocity
local rotationMagnitude = Vector3.new(2, 2, 2)  -- Mild shaking strength for angular velocity

-- Function to print debug info
local function debugPrint(part, message)
    print("[DEBUG] " .. part.Name .. ": " .. message)
end

-- Function to shake the part with slight random rotation and movement
local function shakePart(part)
    if part:IsA("BasePart") then
        -- Slight random translation shake
        local randomShake = Vector3.new(
            math.random(-shakeMagnitude.X, shakeMagnitude.X),
            0,  -- No vertical shake (to keep it grounded)
            math.random(-shakeMagnitude.Z, shakeMagnitude.Z)
        )
        
        -- Slight random rotation shake
        local randomRotation = Vector3.new(
            math.random(-rotationMagnitude.X, rotationMagnitude.X),
            math.random(-rotationMagnitude.Y, rotationMagnitude.Y),
            math.random(-rotationMagnitude.Z, rotationMagnitude.Z)
        )
        
        debugPrint(part, "Shaking part with slight movement and rotation...")

        -- Apply mild shaking velocities for translation and rotation
        part.AssemblyLinearVelocity = randomShake
        part.AssemblyAngularVelocity = randomRotation
    end
end

-- Function to destroy all welds in a part (only once per part)
local function destroyAllWelds(part)
    local weldFound = false
    -- Only destroy welds once per part to avoid redundancy
    if part:FindFirstChild("WeldsDestroyed") == nil then
        for _, weld in ipairs(part:GetDescendants()) do
            -- Check for any type of weld (e.g., WeldConstraint, Weld)
            if weld:IsA("WeldConstraint") or weld:IsA("Weld") then
                debugPrint(part, "Destroying weld: " .. weld.Name)
                weld:Destroy()
                weldFound = true
            end
        end

        -- Add a marker to the part so we don't destroy the welds again
        local weldsDestroyedMarker = Instance.new("BoolValue")
        weldsDestroyedMarker.Name = "WeldsDestroyed"
        weldsDestroyedMarker.Parent = part

        if not weldFound then
            debugPrint(part, "No welds found.")
        end
    else
        debugPrint(part, "Welds already destroyed, skipping.")
    end
end

-- Function to unanchor parts
local function unanchorPart(part)
    if part:IsA("BasePart") and part.Anchored then
        debugPrint(part, "Unanchoring part...")
        part.Anchored = false -- Unanchor the part
    end
end

-- Function to check if a part is "Breakable"
local function isBreakable(part)
    local breakable = part:FindFirstChild("Breakable")
    if breakable and breakable:IsA("BoolValue") and breakable.Value == true then
        debugPrint(part, "Part is breakable.")
        return true
    else
        debugPrint(part, "Not breakable.")
        return false
    end
end

-- Function to handle parts during the earthquake
local function handleParts(parts)
    for _, part in ipairs(parts) do
        if isBreakable(part) then
            debugPrint(part, "Part is breakable, proceeding to destroy welds and unanchor.")
            destroyAllWelds(part) -- Destroy all welds in the part (only once)
            unanchorPart(part)    -- Unanchor the part
            shakePart(part)       -- Apply slight shaking effect
        end
    end
end

-- Function to detect parts using Region3
local function detectPartsUsingRegion3()
    -- Calculate Region3 based on the main part's position and size
    local partSize = mainPart.Size
    local regionSize = partSize + Vector3.new(15, 15, 15)  -- Adjusted region size for better detection
    local minBound = mainPart.Position - regionSize / 2
    local maxBound = mainPart.Position + regionSize / 2
    local region = Region3.new(minBound, maxBound)

    -- Get all parts in the Region3 (ignoring the main part itself)
    local partsInRegion = workspace:FindPartsInRegion3(region, mainPart, math.huge)

    if #partsInRegion == 0 then
        debugPrint(mainPart, "No parts found in Region3.")
    else
        debugPrint(mainPart, "Parts found in Region3!")
        handleParts(partsInRegion)  -- Handle all parts in the region
    end
end

-- Main earthquake function
local function startEarthquake()
    local startTime = tick()

    while tick() - startTime < shakeDuration do
        detectPartsUsingRegion3() -- Detect and handle parts using Region3
        wait(shakeInterval) -- Wait before applying the next shake to reduce load
    end
end

-- Start the earthquake when this script runs
startEarthquake()

the bugs:
Its shaking but the welds aren’t breaking on touch.
also the main part is non coliable (Which is needed in the situation I’m building in, as the floor breaks)

You can just use,

BasePart:BreakJoints()

To break any welds, weld constraints, motor6ds that’s connected to the part

this flew right over my head lol i remembered that was thing :rofl:

1 Like