Weld not a valid member even though it is

Hello!

I’m attempting to make a script where it finds a random part and deletes it’s welds. The issue is, it states that Weld is not a valid member, even though it does exist. I tried to put a WaitForChild, but it infinite yielded. Other posts weren’t relevant or didn’t work. Any help is appreciated!

local Model = workspace.Grassland

local function DeleteRandomPart()
	local ChildrenOfModel = Model:GetChildren()
	local PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)]
	local Weld = PartToDelete:WaitForChild("Weld")
	if Weld then
		if PartToDelete.Name == "LandPart" then
			Weld:Destroy()
		end
	end
end

for count = 1, #Model:GetChildren() do 
	DeleteRandomPart()
	wait(.1)

	if not #Model:GetChildren() then
		Model:Destroy()
		break
	end
end

Try

local Weld = PartToDelete:FindFirstChildOfClass("Weld")
if Weld and PartToDelete.Name == "LandPart" then
	Weld:Destroy()
end

Didn’t work, in fact broke the whole script.

Are there any errors in the output?

No errors, the script somehow stopped.

Your script iterates all children of the model. If not all parts of the model have welds or are named “LandPart” then the script will just skip over them with a 0.1 second wait. Try removing the wait(.1) and see if it breaks all welds

Nothing’s happening. I only want the script to break the welds of certain parts.

local Model = workspace.Grassland

local function DeleteRandomPart()
	local ChildrenOfModel = Model:GetChildren()
	local PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)]
	local Weld = PartToDelete:FindFirstChildOfClass("Weld")
	if Weld and PartToDelete.Name == "LandPart" then
		Weld:Destroy()
	end
end

for count = 1, #Model:GetChildren() do --repeats the line below for the number of children in the model
	DeleteRandomPart()
	wait(1)

	if not #Model:GetChildren() then
		Model:Destroy()
		break
	end
end

You could try something like this

local Model = workspace.Grassland
local Parts = {}

-- store all parts named LandPart in array
for i, Part in pairs(Model:GetChildren()) do
	if Part.Name == "LandPart" then
		table.insert(Parts, Part)
	end
end

-- delete part from the array of stored parts
local function DeleteRandomPart()
	local PartToDelete = table.remove(Parts, math.random(#Parts))
	local Weld = PartToDelete:FindFirstChildOfClass("Weld")
	if Weld then
		Weld:Destroy()
	end
end

for i = 1, #Parts do
	DeleteRandomPart()
	if #Parts <= 0 then
		Model:Destroy()
		break
	end
	wait()
end

Nope, nothing. No errors at all.

Can you show the hierarchy of “Grassland” and the parts and welds inside?

Screenshot (3)

That’s only a portion of the model, it’s the same thing

Okay try this and lmk what it says in the output

local Model = workspace.Grassland
local Parts = {}

-- store all parts named LandPart in array
for i, Part in pairs(Model:GetChildren()) do
	if Part.Name == "LandPart" then
		table.insert(Parts, Part)
	end
end

print("Number of parts:", #Parts)

-- delete part from the array of stored parts
local function DeleteRandomPart()
	local PartToDelete = table.remove(Parts, math.random(#Parts))
	local Weld = PartToDelete:FindFirstChildOfClass("Weld")
	if Weld then
		Weld:Destroy()
		print'Destroyed Weld'
	end
end

for i = 1, #Parts do
	DeleteRandomPart()
	if #Parts <= 0 then
		Model:Destroy()
		print'Destroyed Model'
		break
	end
	wait(0.2)
end

Number of parts: 323 (((((((((((((((((((((((((

Does it print “Destroyed Weld”? Maybe it just takes a while for you to notice it deleting any welds since it’s 323 parts

It sayd “Destroyed Weld” even though the welds are still there.

With wait(0.2) it would take 64.6+ seconds to delete all welds, are you sure it’s not deleting any welds at all?

When it said “Destroyed Weld”, the welds were still there. I waited a few more seconds, welds weren’t deleted.

If this is what I think it is, you can do a for loop to delete the welds of a random part

for _,v in pairs(randompart:GetChildren()) do
if v:IsA(“Weld”) then
v:Destroy()
end
end)

Nope, didn’t work either. Only the original script works?

It’s deleting the welds fine for me. Remove the wait() completely and see if the welds are still there