Welds

From Legacy Roblox Wiki
Revision as of 11:42, 3 July 2008 by >Mindraker (Category:Scripting Tutorials yeah, this needs some work, too.)
Jump to navigationJump to search

No, these are not the kind of weld where you take the weld tool and put two bricks together. This is serious welding, used for multi-handled weapons (such as xLEGOx's NXT Gen Weapons), and much, much more. Now, here's a bit on welds:

Prerequisite: Know how to use a basic cframe (CFrame.new(#, #, #)), and how to index and change the properties of an object, using a script.

Part I : Welds.

What are welds? They are an object, that hold their parent part, and another part, in a position relative to each other. For example, hold one part 2 units to the right of the other brick. They can be used in places where parts need to be held together at odd angles, but still be able to move, such as for a vehicle.

Basic structure: The basic structure of a weld is as follows: the weld object is placed inside of a part, and a property is set to determine another part which should be welded to the original part. Then two CFrames, the C0 and the C1, tell the weld how the parts should be placed.

Part0: This property of a weld is an Object, it must always be set to the part the weld is in for the weld to work


Part1: This property of a weld is an object and tells is which part it should be attaching its parent to

C0: the C0 of a weld, determines how the [offset point] should be attached to the Part0

C1: the C1 of a weld, determines how the Part1 should be attached to the [offest point]

Setting the values: Figuring out what to set the C0 and C1 to is a bit finicky, but one you get good at it, it can go quite quickly. For Welds, you dont have to worry about the C1, it's automaticaly set to a "unit" or unrotated CFrame, you only have to deal with C1 when working with motors, so ignore it for now. The C0 will tell the weld how it should attatch itself to the other part, eg.

[weld].C0 = CFrame.new(0, 2, 0)

This tells the weld that it should hold the part1 in a position 2 studs above the part0, simple eh?

Rotating the CFrame: Now it gets a bit tricky. To rotate the CFrame you must use the following command, which is case sensitive... CFrame.fromEulerAnglesXYZ(#, #, #)

Which is used as follows: [weld].C0 = CFrame.new(0, 2, 0)*CFrame.fromEulerAnglesXYZ(0, math.pi, 0)

this tells the weld to attach the part1, two studs above the part0 AND rotate the part1 by 180 degrees relative to the part0

math.pi?? Yes you use math.pi, as the number to rotate by, use it in the following fashion :

  • math.pi = 1/2 of a turn
  • math.pi/2 = 1/4 of a turn
  • math.pi/4 = 1/8 of a turn

etc..

Putting it all together : Well, not that you know how to use the basics of a weld, here's how you put it all together. Here's an example:

Lets say that this is in a script, in a vehicle:

pln = script.Parent 

local w = Instance.new("Weld") 
w.Parent = pln.Engine 
w.Part0 = pln.Engine --> part0 has to be the parent 
w.Part1 = pln.Wing1 
w.C0 = CFrame.new(0, 0, 6)*CFrame.new(0, -math.pi/5, 0)