WeldConstraint weld to player offset

I’m using a weldConstraint to weld items to the player in my game, I set the weldconstraint’s CFrame using SetPrimaryPartCFrame then set up the Part0 and Part1 after, but since the player is moving it causes weird offsets. Any way to code it correctly using WeldConstraints?

The code I am using to attempt to weld it correctly:

item.Parent = player.Character
item:SetPrimaryPartCFrame(player.Character.UpperTorso.CFrame*CFrame.new(0,0.4,-1.3))
weld.Part0 = player.Character.UpperTorso
weld.Part1 = item.PrimaryParts

What it shows when the plr moves while attempting to weldConstraint:
image

What It shows if the plr doesn’t move when welding:
image

3 Likes

Code looks fine. Can you attach an empty baseplate .rbxl file which has just this code and demonstrates the issue?

Sure thing. I’ll get that now.

ConstraintTest.rbxl (15.2 KB)

Keep in mind the issue only occurs on a real server, not in play solo.
Press Space to get cheese welded to you.
FilteringEnabled is on.

2 Likes

Same thing at my place. I uploaded the same exact file to my place, played the game, pressed space, and it happens here too. I do not know why though.

1 Like

It looks like the issue is because WeldConstraints are simulated individually between clients and the server, rather than having their values synced across everything.

Left: Server, Right: Client

In a test server and in-game, there’s some amount of latency, so the character appears in one location on the server while it’s slightly somewhere else on the client. Since the cheese is created on the server, it uses the server’s position of the character – this is why the cheese looks as it should on the server. However, since the cheese’s position is not lined up with where the client thinks the character is, you get odd results like this.

This doesn’t seem like good behavior. I’ll make a feature request for the offset to sync across the server/clients instead of being simulated disjointly. I would recommend using normal Welds in the meantime.

9 Likes

Hmm alright I’ll see what I can do to replace it to a normal weld.

1 Like

I was experiencing an issue like this with a character model I had put together. The WeldConstraints would be offset outrageously upon creating the character.

For those in a rush, here is a quick and dirty script that can convert WeldConstraints on an already existing model to Welds.

local baseModel = workspace.MODELNAME
local model = baseModel:Clone()
baseModel.Name = baseModel.Name .. "_Old"

local function weldBetween(a, b)
	local weld = Instance.new("Weld", a)
	weld.C0 = a.CFrame:inverse() * b.CFrame
	weld.Part0 = a
	weld.Part1 = b
	return weld
end

for _,constraint in pairs(model:GetDescendants()) do
	if constraint:IsA("WeldConstraint") then
		
		local a = constraint.Part0
		local b = constraint.Part1
		if a and b then
			weldBetween(a, b)
		end
		
		constraint:Destroy()
		
	end
end

model.Parent = baseModel.Parent
5 Likes

I’m currently having a similar issue.

I’m using WeldConstraints to attach a gun (model) to the player using 3 main parts;
headAnchorPart - this is CFramed to camera.CoordinateFrame
gunAnchorPart - this is used to offset the gun from the headAnchorPart - the centre point for the gun’s handle.
gunHandle - the actual handle of the gun.

This is what it should like - Red being = headAnchor, green = gunAnchor, and pink = handle
The attachments represents the different states of offset of the gun from the camera (holding, running, iron-sights)

Instead of the the headAnchor cframing to the cam’s coordinate frame though - it keeps offseting, as if it is centring on the middle point of all connected parts:

Whether I’m missing something completely or this is a bug - I don’t know but this is very strange.
I expected that the applied cframe be respected, not offset by the connected parts.

For now I’ll try to use legacy welds, but if this could be fixed in the future, that would be awesome - WeldConstraints are way easier to work - besides the odd bug.

1 Like

This is still an issue 2 years after this being posted. I can’t stress it enough; The offsets caused by latency between the server and its clients when using WeldConstraints is absolutely a bug.

2 Likes

Report it them via #platform-feedback:engine-bugs if you genuinely think it’s a bug. Posting here does nothing good except bump a thread from over a year ago.

1 Like

Do you have a repro for issues you’re seeing?

I tested the ConstraintTest.rbxl both with test server with extra simulated latency and as a published game and I can’t reproduce the bug.

1 Like

Yup, a similar issue still occurs in our game as well.

local Weld = {}

local function manualWeld(partA, partB)
	assert(typeof(partA) == "Instance" and partA:IsA("BasePart"), "partA needs to be a Base Part.")
	assert(typeof(partB) == "Instance" and partB:IsA("BasePart"), "partB needs to be a Base Part.")
	
	local weld = Instance.new("ManualWeld")	
	weld.C0 = partA.CFrame:toObjectSpace(partB.CFrame)
	weld.Part0 = partA
	weld.Part1 = partB
	weld.Parent = partA
end

-- Currently does not work on character reset
local function weldTo(partA, partB)
	assert(typeof(partA) == "Instance" and partA:IsA("BasePart"), "partA needs to be a Base Part.")
	assert(typeof(partB) == "Instance" and partB:IsA("BasePart"), "partB needs to be a Base Part.")
	
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = partA
	weld.Part1 = partB
	
	weld.Parent = partA
end

function Weld.WeldModel(character, clonedModel, characterModel)
	local characterContent = character:GetChildren()

	for _, bodyPart in pairs(characterContent) do
		local modelPart = clonedModel:FindFirstChild(bodyPart.Name)
		if modelPart then
			local offsetCFrame = characterModel[bodyPart.Name].CFrame:toObjectSpace(characterModel[clonedModel.Name][modelPart.Name].CFrame)
			modelPart.CFrame = bodyPart.CFrame * offsetCFrame
			manualWeld(modelPart, bodyPart)
			--weldTo(modelPart, bodyPart) -- Does not work on character reset
		end
	end
	
	clonedModel.Parent = character
end

return Weld

Demonstration of the issue:

WeldConstraint works fine the first time a game character is applied to the player’s character.
But from the second time onwards, there is an offset when using the WeldConstraint, ManualWeld fixes this issue and works fine.

I hope this can be looked into.

EDIT: Added video to demonstrate issue.
EDIT 2: After looking around on the devforum some more, I shouldn’t use WeldConstraints for characters, because those welds are most likely to be used for static offsets only.