Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I weld a part's position but not its rotation?

Howdy. I am welding a gun to my character's right hand. It cannot be a tool for many reasons, first and foremost that it needs to rotate to face my mouse (think Metroid Prime). This is the code inside my gun:

wait(2)
playerTable = game.Players:GetChildren()
player = playerTable[1]
print(player)
mouse = player:GetMouse()
weld = Instance.new("Weld")
weld.Parent = script.Parent
weld.Part0 = weld.Parent
weld.Part1 = player.Character["Right Arm"]
weld.C0 = CFrame.new(0,1,0)
while true do
	wait()
	script.Parent.CFrame = CFrame.new(script.Parent.CFrame.p, mouse.Hit.p)
end

As you would expect, this code welds the gun to the player's right arm one stud up. The problem comes with the CFrame's second vector. It works perfectly, sort of: the gun does rotate towards the mouse. The problem? It is welded to my character, so my character faces my mouse as well. Any help is appreciated. Thanks in advance.

1 answer

✓ Accepted answer

In your place I would use Constraints rather than welding, because they allow for movement / rotation: http://wiki.roblox.com/index.php?title=Constraints . Seems that Ball in Socket Constraint would do for what you're trying to do: http://wiki.roblox.com/index.php?title=API:Class/BallSocketConstraint . It will allow both parts to rotate freely, so you'll just have to adjust gun's rotation.

2 comments

0

That sounds promising. I'll look into that

0

That worked phenomenally! Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.