Part not Welded Correctly

I’m trying to make a soda jug from ServerStorage “attach” to the player’s UpperTorso, similar to how the backpacks are in games like Bee Swarm Simulator, but for some reason, the jug isn’t welded. It just falls through the baseplate, like it’s welded to nothing.

Here’s the code.

--Variables
local players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")

--Attaching jug
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local jug = serverStorage.SodaJug:Clone()
		local weld = Instance.new("WeldConstraint")
		weld.Enabled = true
		jug.CFrame = char:WaitForChild("UpperTorso").CFrame:ToWorldSpace(CFrame.new(Vector3.new(0, 0, 2.894)))
		jug.Parent = char
		weld.Part0 = char.UpperTorso
		weld.Part1 = jug
		weld.Parent = char.UpperTorso
		weld = nil
		jug = nil
	end)
end)

There are no errors, and it’s not like the jug doesn’t appear at all, I can see the jug in workspace,but it falls and gets destroyed like it isn’t welded.

  1. What do you want to achieve? I want to weld a part to the player’s torso.

  2. What is the issue? The part appears in workspace, but isn’t attached to the torso, as if it isn’t welded at all.

  3. What solutions have you tried so far? I tried mixing up the order of the code, and tried setting the position instead of CFrame, but nothing worked.

Edit: I’ve noticed that the weld is only briefly parented to the UpperTorso. It strangly gets destroyed for some reason.

2 Likes

Try removing these lines:

weld = nil
jug = nil

That won’t do anything, and will in fact, make the script less efficient. All those lines do is set the unneeded variables to nil, which doesn’t destroy the object they’re assigned to or anything.

You’re setting the weld object to nil which is basically destroying it. Remove the line that sets weld to nil and test it.

Do the same thing with the jug object.

weld is a variable. Setting it to nil doesn’t destroy the object it’s assigned to, it just makes it so more memory is available, and I won’t be able to access the object in the rest of the event, nothing else happens.

If this weren’t the case, then I wouldn’t be able to briefly see the jug in workspace.

So you’re saying that setting weld to nil will get you more memory and it will work just the same? That makes no sense.

I think I’ve seen the problem.
You’re setting the jug’s cframe before parenting it into the character and I don’t think that’s possible. Try this:

--Variables
local players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")

--Attaching jug
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local jug = serverStorage.SodaJug:Clone()
        jug.Parent = char
		local weld = Instance.new("WeldConstraint")
		weld.Enabled = true
		jug.CFrame = char:WaitForChild("UpperTorso").CFrame:ToWorldSpace(CFrame.new(Vector3.new(0, 0, 2.894)))
		weld.Part0 = char.UpperTorso
		weld.Part1 = jug
		weld.Parent = char.UpperTorso
		weld = nil
		jug = nil
	end)
end)

That still didn’t work, sorry.

I’ve never experienced this problem before, so I have no idea what’s going on.

I’ve just noticed that the weld is only parented to the UpperTorso for less than a second, after that, it seems to be destroyed for some reason. Removing weld = nil didn’t fix it, by the way.

Welp, I’ve got to go to bed now. I’ll try to help you in the morning if your issue hasn’t been fixed.

1 Like

I don’t think anythings wrong with your script. I’ve tested it and it seems to work fine:

It might be something with the actual “SodaJug” part/model.

If it helps, it’s a MeshPart, and the only thing inside it is an Attachment, and in the Attachment is a ParticleEmitter.

Not related to your problem, but you don’t need to do weld = nil and jug = nil as they will get GCed once that scope is finished.

It still works for me. Are you sure there isn’t another script messing with the player or the SodaJug part?

Also if you want, could you send the soda jug model too?

There’s only two scripts in the game, one is a LocalScript, and doesn’t even touch the jug, and the other is a server script, which only creates the jug, and is the script that is broken.

Also, the problem seems to be that the weld only works for less than a second, because it is very shortly destroyed.

Can you print the children’s name of the JUG? It seems like you didn’t debug enough.

You can also print the Part0 and Part1 property of the weld.

The children of the jug has nothing to do with this, but it does exist if you’re wondering.

So I’ve printed pPart0 and Part1 both immediately, and after 2 seconds, and after two seconds, Part0 was nil, but Part1 was still jug.

I printed just before the variables setting to nil.

This probably means something happened to the UpperTorso, but could you try to weld the Jug to the HumanoidRootPart instead?

It’s successfully welded to the HumanoidRootPart, so the problem must be the UpperTorso. Is there anything in the UpperTorso that might be destroying the weld?