Weld Constraints aren't working

I’m just trying to move a model to the place you clicked but it only moves the hitbox, not the entire model.
image

Scripts :

-- Module function (yes, it prints "a")

function module:moveStructure(Pos:Vector3,rotationY:number,Structure:Model,Tycoon:Folder)
	if Structure == nil or not Storage.Structures:FindFirstChild(Structure.Name) or (Structure and not Structure:IsA('Model')) then return end
	print('a')
	Pos = Vector3.new(Pos.X,(Structure.PrimaryPart.Size.Y / 2) + 1,Pos.Z)
	
	local list = {  }

	Structure.PrimaryPart.Anchored = true
	
	for _, Part:BasePart in Structure.Build:GetDescendants() do
		if Part:IsA('BasePart') then
			local weld = Instance.new('WeldConstraint',Part)
			
			weld.Part0 = Structure.PrimaryPart
			weld.Part1 = Part
			
			list[Part] = weld
			
			Part.Anchored = false
		end
	end
	
	Structure.PrimaryPart.CFrame = CFrame.new(Pos) * CFrame.fromOrientation(0,math.rad(rotationY),0)
	
	for Part, Weld in list do
		Part.Anchored = true
		
		Weld:Destroy()
	end
end
-- script that uses it (this is inside a remote event function)

local offset = Vector3.new(0,0,0)

if selectedStructure.Properties.Offset.Value then offset = Vector3.new(.5,0,.5) end
		
local Clone = selectedStructure:Clone()

Module:moveStructure(Hit,rotationY,Clone,Plr.tycoon.Value)
		
--Clone:PivotTo(CFrame.new(Hit.X,(Clone.PrimaryPart.Size.Y / 2) + 1,Hit.Z) * CFrame.fromOrientation(0,math.rad(rotationY),0) + offset)
		
Clone.Parent = Plr.tycoon.Value.Structures
		
for _, touching:BasePart in workspace:GetPartsInPart(Clone.PrimaryPart) do
	if not touching:IsDescendantOf(Clone) and touching:IsDescendantOf(Plr.tycoon.Value) and touching.Name == 'Hitbox' then
		Clone:Destroy()
				
		return
	end
end

Plr.leaderstats.Cash.Value -= selectedStructure.Price.Value

I also tried looking at the devforum, but all of them said something like

The primary part should be anchored while all the others should not be anchored.

which is what I am already doing.

1 Like

Try doing

Structure:SetPrimaryPartCFrame(CFrame.new(Pos) * CFrame.fromOrientation(0,math.rad(rotationY),0))

which moves the whole model instead of

Structure.PrimaryPart.CFrame = CFrame.new(Pos) * CFrame.fromOrientation(0,math.rad(rotationY),0)

which only moves the model.PrimaryPart and doesn’t account for weld constraints.

If you use

TweenService:Create(Structure.PrimaryPart, TweenInfo.new(1), {CFrame = CFrame.new(Pos) * CFrame.fromOrientation(0,math.rad(rotationY),0)}):Play()

weld constraints will be accounted for.

1 Like

This actually worked, but :SetPrimaryPartCFrame() is deprecated, is there anything else similiar to it other than :PivotTo()? I don’t really like using deprecated things.

Edit : The TweenService one also didn’t work.

Oh yeah I forgot that it was depricated, sorry I don’t know any other method other than :PivotTo(). Btw only use TweenService if you want a smooth motion between the two points.

1 Like

After messing around with :ToObjectSpace() and :ToWorldSpace() for quite a while now, I finally managed to come up with a solution that works.

function main:moveStructure(Pos:Vector3,rotationY:number,Structure:Model)
	if Structure == nil or (Structure and not Storage.Structures:FindFirstChild(Structure.Name)) then return end
	
	Pos = Vector3.new(Pos.X,(Structure.PrimaryPart.Size.Y / 2) + 1,Pos.Z)
	
	local goal = CFrame.new(Pos) * CFrame.fromOrientation(0,math.rad(rotationY),0)
	
	for _, Part:BasePart in Structure.Build:GetDescendants() do
		if Part:IsA('BasePart') then
			local offset = Structure.PrimaryPart.CFrame:ToObjectSpace(Part.CFrame)
			
			Part.CFrame = goal:ToWorldSpace(offset)
		end
	end
	
	Structure.PrimaryPart.CFrame = goal
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.