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.
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.