How to make cframe.lookat work with welds?

So i want to make it so that cframe.lookat works with welds but here is what happens


image
Help would be apreciated, here is the code:

while task.wait() do
	script.Parent:PivotTo(CFrame.lookAt(script.Parent:GetPivot().Position, Vector3.new(script.Parent:GetPivot().Position.X, workspace.Part.Position.Y, workspace.Part.Position.Z)) * CFrame.Angles(0,math.pi,0))
end

The issue you’re experiencing with the CFrame.lookAt function and welds is likely due to the fact that the lookAt function expects vectors as input, rather than specific parts or welds.

To make CFrame.lookAt work with welds, you’ll need to obtain the desired target position from the weld’s attachment points or the position of the desired part. Here’s an example of how you can modify your code to make it work:

local weld = script.Parent -- Assuming script.Parent is the weld you want to pivot

while true do
	local targetPosition = workspace.Part.Position -- Replace 'workspace.Part' with the actual part you want to look at
	local pivot = weld:GetPivot()
	local lookAtCFrame = CFrame.lookAt(pivot.Position, targetPosition)
	local rotatedCFrame = lookAtCFrame * CFrame.Angles(0, math.pi, 0)
	weld:PivotTo(rotatedCFrame)
	wait()
end

In this code, we first obtain the target position you want the weld to look at (in this case, workspace.Part.Position). Then, we calculate the lookAt CFrame using the weld’s pivot position and the target position. Finally, we apply an additional rotation of 180 degrees around the y-axis using CFrame.Angles to make the weld face the target in the opposite direction.

Make sure to replace 'workspace.Part' with the actual part you want to look at.

By using this modified code, the weld should correctly rotate and look at the desired target position.

I tried it, doesnt work. From what i see it seems to be the same code but just spread out in multiple lines. Not sure tho im not an experienced coder.

Nice animation stealer plugin you got running in the background there. Anyway, for your use case you should be editing the Weld’s C1 property instead of trying to Pivot the TurretBody.

Thanks, and also the only time i use it is for those free model kits with animations that roblox for some reason doesnt allow animations for other people, last time used was like 2 months ago. I don’t steal games animations.

Tried what you said. Funny stuff is happening

	script.Parent.Parent.TurretBody.Barrel.C1 = CFrame.lookAt(script.Parent:GetPivot().Position, Vector3.new(script.Parent:GetPivot().Position.X, workspace.Part.Position.Y, workspace.Part.Position.Z)) * CFrame.Angles(0,math.pi,0)

Weld the barrel to the TurretBase

I did. It just does funny stuff. Welding isnt the issue its the code

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