Collapsing Structure script

I’ve created this script that produces welds in a structure. It then deletes those welds to give the effect that the structure is collapsing.

However, after the welds are deleted, it gives me this error to which I have tried countering so many times. I’m not sure what to do here. I’ve even tried wrapping the error into a conditional statement, but to no avail. The welds are placed within a single Part called Base.

local structureDebris = workspace.StructureTest:GetChildren()
local structurePrimary = workspace.StructureTest.PrimaryPart

for k = 1,#structureDebris do
	if structureDebris[k]:IsA("Part") or structureDebris[k]:IsA("WedgePart")then
		local weld = Instance.new("Weld")
		weld.Part0 = structurePrimary
		weld.Part1 = structureDebris[k]
		weld.C0 = structurePrimary.CFrame:Inverse()
		weld.C1 = structureDebris[k].CFrame:inverse()
		weld.Parent = structurePrimary
	end
end

while true do
	wait(.5)
	if workspace.StructureTest.Base:GetChildren() ~= nil then
		workspace.StructureTest.Base.Weld:Destroy() --Problem here
	elseif workspace.StructureTest.Base:GetChildren() == nil then
		break
	end
end

Basically in that line you’re assuming that the weld exists, to avoid that you could simply check before doing anything to it. you can use the following:

 if workspace.StructureTest.Base:FindFirstChildOfClass("Weld") then
 
workspace.StructureTest.Base:FindFirstChildOfClass("Weld"):Destroy()

end

or simply do

workspace.StructureTest.Base:BreakJoints()
2 Likes

Oh yeah and i recommend you to use

game:GetService("Workspace")

workspace is as valid a method of accessing the workspace as any other method, as described in the official documentation.
https://developer.roblox.com/en-us/api-reference/class/Workspace

1 Like

I don’t know if this will solve your problem directly but I suggest turning you welds into weld constraints because they are easier to manage and use.

AFAIC, sometimes using “workspace” in LocalScripts (although this is a ServerScript case) RARELY used to return nil, but i think things have changed. anyway, thank you

Using workspace instead of game:GetService(“Workspace”)

Has proven to be buggy

Buggy in what sense exactly? If you’re talking about the service not being available when referencing the workspace global, I wouldn’t consider that a bug. Its more of an issue with limitations on how fast things can replicate in.

When I have tried to use workspace directly it would just return and not run the rest of my code. It makes no sense, but when I changed it to game:GetService(“Workspace”) it worked completely fine.

Wow! I didn’t even know that FindFirstChildOfClass() existed! I just keep learning more and more new stuff.

Thanks a lot! 8)