Hi!
I’m making a weld destroying system, but the welds won’t destroy.
Here is the script:
--IF YOU DON'T WANNA CHANGE ON THE SCRIPT, PUT IT INSIDE THE MODEL--
local amount = 1568 --CHANGE NUMBER TO HOW MANY WELDS YOU HAVE (IT WILL PRINT OUT WHEN RUNNING THE GAME)
--EQATION FOR HOW MANY WELDS YOU WANT LEFT TO MAKE THE BUILDING COLLAPSE
local addition = amount / 3
local equation = amount / 2 + addition
--print(equation)
local timerMax = amount / 1.3
local timer = 0
local build = script.Parent
local selection = game.ReplicatedStorage.SelectedDisasters:GetChildren()
local Debounce = false
local Debounce2 = false
-- Function to connect to the "Earthquake" event
local function Rumble()
if not Debounce2 then
local minAngle = -360
local maxAngle = 360
local randomAngle = math.random(minAngle, maxAngle)
local radians = randomAngle * (math.pi / 180)
local speed = 5
local xVelocity = speed * math.cos(radians)
local zVelocity = speed * math.sin(radians)
Debounce2 = true
for _,part in ipairs(build:GetChildren()) do
if part then
if part:IsA("Part") or part:IsA("UnionOperation") or part:IsA("MeshPart") or part:IsA("BasePart") then
local newVelocity = Vector3.new(xVelocity,0,zVelocity)
part.Velocity = newVelocity
end
end
end
wait(2)
Debounce2 = false
end
end
local function startRumble()
game["Run Service"].Heartbeat:Connect(Rumble)
end
local function collapse()
wait(2)
startRumble()
for _,v in ipairs(build:GetDescendants()) do
if v and v:IsA("Weld") then
if timer <= timerMax then
timer = timer + 1
wait(math.random(0.1,0.15))
print(v)
v:Destroy()
end
end
end
end
local function ConnectToEarthquake()
local earthquakeEvent = game.ReplicatedStorage.SelectedDisasters:FindFirstChild("Earthquake")
if earthquakeEvent and earthquakeEvent:IsA("RemoteEvent") then
if script.Parent.Parent == game.Workspace.ShownMaps then
if not Debounce then
game.ReplicatedStorage.SelectedDisasters.Earthquake.OnServerEvent:Connect(function()
Debounce = true
collapse()
wait(70)
Debounce = false
end)
end
end
end
end
-- Connect to the "Earthquake" event outside the Heartbeat event
game["Run Service"].Heartbeat:Connect(function(deltaTime)
ConnectToEarthquake()
-- You can perform other logic here related to the Heartbeat event if needed
end)
Any help appreciated!
Edit: the print(v) works fine and it iterates perfectly through all welds, they just resist getting destroyed.
I would probably start with debugging. Prints in different parts and see what prints and what doesn’t.
local function collapse()
wait(2)
--print
startRumble()
--print
for _,v in ipairs(build:GetDescendants()) do
--print
if v and v:IsA("Weld") then
--print
if timer <= timerMax then
--print
timer = timer + 1
--print and so on.
wait(math.random(0.1,0.15))
print(v)
v:Destroy()
end
end
end
end
Are you sure it’s a weld, and are there any other constraints like a weldconstraint?
Are the builds anchored, it seems like you’re making an earthquake, destroying welds will not make a difference if it’s anchored.
Are weld created via a script (if so and it’s a local script then it wouldn’t be replicated to the server)? Did you actually call the function, I don’t seem to see it called
That Is true, you should print after weld is destroyed to see if it returns nil or not. If it does then it mean it could be what AuthenticatedTroll has said.
Idk, but I have a script related that collapses a building when the welds are low.
--IF YOU DON'T WANNA CHANGE ON THE SCRIPT, PUT IT INSIDE THE MODEL--
local amount = 1568 --CHANGE NUMBER TO HOW MANY WELDS YOU HAVE (IT WILL PRINT OUT WHEN RUNNING THE GAME)
--EQATION FOR HOW MANY WELDS YOU WANT LEFT TO MAKE THE BUILDING COLLAPSE
local addition = amount / 3
local equation = amount / 2 --+ addition
print(equation)
local timerMax = amount / 2
local timer = 0
local model = script.Parent
local debounce = false
--FUNCTION FOR CHECKING HOW MANY WELDS ARE IN YOUR BUILDING
local function checkWelds()
local weldCount = 0
for i, v in ipairs(model:GetDescendants()) do
if v and v:IsA("Weld") then
weldCount = weldCount + 1
end
end
return weldCount
end
print(checkWelds())
local function collapse()
if not debounce then
debounce = true
--MAKES WELDS DESTROY IF THE TIMER IS SMALLER THAN THE MAXIMUM NUMBER AND IF THERE ARE WELDS
for i,v in ipairs(model:GetDescendants()) do
if v and v:IsA("Weld") then
if timer <= timerMax then
wait(math.random(0.1,0.15))
timer = timer + 1
v:Destroy()
print("Destroyed a weld.")
end
end
end
--DESTROYS PRIMARY SO ANY CHUNKS LEFT OF THE BUILDING FALLS TOO
if script.Parent.Primary then
script.Parent.Primary:Destroy()
print("Primary Destroyed")
end
--DELETE THIS V
for i,v in ipairs(script.Parent:GetChildren()) do
if v and v:IsA("Part") then
if v.Name == "Roof" then
for i,weld in ipairs(v:GetChildren()) do
if weld and weld:IsA("Weld") then
weld:Destroy()
end
end
end
end
end
--DELETE THIS ^^^
end
end
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
local totalWelds = checkWelds()
if totalWelds <= equation then --CHECKS IF THE BUILD IS READY TO COLLAPSE
collapse()
end
end)