How to make this i, v in pairs weld loop work?

As the title says, I have been trying to use a script to temporarily make some models welded to another part for some time. I am fairly new to coding, so I am not sure if what I am writing is correct or not. The idea is that each model has parts, which are all welded to one main part in the model. This main part is not welded, instead attached to a HingeConstraint. I want to weld each of these main parts to another part through a script, but for some reason, the script only welds one of these models each time I run the game. My code is here:

local heart = game.Workspace.HEART.SHIELD
local cage = game.Workspace.CAGE
local clockfacegear = game.Workspace.HEART:GetDescendants() 

if cage.CanCollide == true then
	local Weld = Instance.new("WeldConstraint")
	for i, v in pairs(clockfacegear) do
		if v:IsA("BasePart") then
			if v.Name == "Main" then
			Weld.Part0 = v
			Weld.Part1 = heart
			Weld.Parent = v

Apologies if this is not informative enough, I am honestly not totally sure what I am doing in general, so if this is too vague do tell (if you want). Thanks in advance!

2 Likes

you only create one weld constraint instance, which is set to a different part each time, meaning only one (thing) will get welded

try making a new weld constraint for each part (under the check for basepart)

2 Likes

Ahh gotcha, how would I do that by chance?

1 Like

He means putting the weld variable inside the loop. Therefore a weld created for each part. Try this:

local heart = game.Workspace.HEART.SHIELD
local cage = game.Workspace.CAGE
local clockfacegear = game.Workspace.HEART:GetDescendants() 

if cage.CanCollide == true then
	for i, v in pairs(clockfacegear) do
		if v:IsA("BasePart") then
			if v.Name == "Main" then
            local Weld = Instance.new("WeldConstraint") --moved weld variable here
			Weld.Part0 = v
			Weld.Part1 = heart
			Weld.Parent = v
2 Likes

Hm, for some reason that does not seem to do the trick, although one of the models is being welded still so that is a plus!

2 Likes

Hm, I am also a beginner scripter but it seems like that should work. Is the shield a model or a part? (You can’t weld to models). Also, if the shield is a part, it seems like the script would weld the shield to itself (since you referenced the shield as the heart variable) and the clockfacegear gets every descendant of the heart (including the shield).

Does this not work? (And are there any errors in the output?)

local heart = game.Workspace.HEART.SHIELD --make sure the shield is a part and not a model
local cage = game.Workspace.CAGE
local clockfacegear = game.Workspace.HEART:GetDescendants() 

if cage.CanCollide == true then
	for i, v in pairs(clockfacegear) do
		if v:IsA("BasePart") and v.Name == "Main" and v.Name ~= "SHIELD" then
            local newWeld = Instance.new("WeldConstraint")
			newWeld.Part0 = v
			newWeld.Part1 = heart
			newWeld.Parent = v
        end
    end
end
2 Likes

The shield is a part, and the code does weld one of the models correctly, but the others still aren’t being welded. Would it be possible to activate and deactivate welds using the pairs loop, like I put welds into the Main parts of each model and just enabled them through scripts, and then later deactivated them?

2 Likes

Honestly, I’m not sure why the others aren’t being welded. Maybe some smart people could help if they find this post. For the enabling and disabling, you can use the enabled property on the weld constraint. To enable/disable all the welds, you can do something like this.

for i, v in pairs("the descendants of your model") do
    if v:IsA("WeldConstraint") then
        v.Enabled = false --this disables each weld and later, you can do "v.Enabled = true" to enable them again
    end
end
2 Likes

Gotcha, I’ll try that, thank you very much for the insight btw!

2 Likes

No problem! Sorry about the issue, some better programmers could probably help you a lot more.

2 Likes

Sure, the issue is that I have three Models, which are all intended to be attached to the main Model by a set of attachments (which form a HingeConstraint), do not CFrame with the rest of the model since they are not welded to the PrimaryPart of the model. I want to temporarily weld them to the object being CFramed until an Event occurs, where I will disable the weld and the Hinged objects will go back to normal.

If all things were working, then the hinged objects would CFrame normally, but only one of the three is being welded, regardless of what code was used.

1 Like

Nah it’s all good, ideas were very helpful, and even if the solution is not reached I still learned a lot so no worries!

2 Likes

The main model is the object called “HEART” at the top of the code, and the other three models are part of the clockfacegear variable:

local heart = game.Workspace.HEART.SHIELD --"HEART in this part is the main model
local cage = game.Workspace.CAGE
local clockfacegear = game.Workspace.HEART.Hinges:GetDescendants() --Hinges in this case is a folder holding  the models
1 Like

Okay, first of all, you need to change your for loop. Instead of for i, v in pairs, you need to change it to for i = 1, #(How ever many children your model has) do loop.

1 Like

Also, there are no ends? An if statement needs an end, just like a for loop needs an end.

local heart = game.Workspace.HEART.SHIELD
local cage = game.Workspace.CAGE
local clockfacegear = game.Workspace.HEART:GetDescendants() 

if cage.CanCollide == true then
	local Weld = Instance.new("WeldConstraint")
	for i, v in pairs(clockfacegear) do
		if v:IsA("BasePart") then
			if v.Name == "Main" then
			Weld.Part0 = v
			Weld.Part1 = heart
			Weld.Parent = v
                      end
              end
     end
end

You need four ends here. Your script shows none. Try checking your output too to see what’s wrong.

1 Like

There are ends, no worries, just there is more superfluous code below it that would be hard to remove through copy-paste.
And I see what you mean by the i = 1, # , but I was not sure how to properly write it, would it look like:

for i = 1,3 do
	wait()
	for i, v in ipairs(clockfacegear) do
		if v:IsA("WeldConstraint") and v.Enabled == false then 
			v.Enabled = true
			wait(1)
		end
	end
1 Like

i = 1, # loop just loops code for a number of times. I’m pretty sure a i,v loop is correct (not entirely sure).

For example,

for i = 1, 10, 1 do --for index = first number, ending number, increment do

This basically starts from 1, runs the code, goes up by 1, runs the code, and continues until it reaches 10. Since the increment is 1, the code will run 10 times.

A code example would be:

for i = 1, 5 do --the increment is defaulted to 1, so you only need to put the increment if it is above 1
    print(i)
end

--[[
prints this:

1
2
3
4
5
]]

I think the i,v loop is correct, but I’m just unsure on why the other parts aren’t getting welded.

2 Likes

Ohhh gotcha, that makes sense!

2 Likes

Oh wow, thank you very much! This is very helpful! Just a few questions:

  • Can I manually set what the first entry in the brand-new table is?

  • How do I utilize a ModuleScript (I have personally never used them thus far)?

1 Like

Gotcha gotcha, just making sure that the object I want the individual parts to be welded to is always going to be the same one! Thank you once again!

1 Like