Omega_PastesBs

How Tools and Their Events work as well as Weld Conversion.

Jul 19th, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.27 KB | None | 0 0
  1. **Keep in mind this answer is very long, take breaks if you may need to, this will further help you understand how Tools work and how Welds work**
  2.  
  3. So i just tested out tools and well basically when you make the Part and name it Handle it will automatically Create a Weld named RightGrip and place it in Right Arm hence RightGrip
  4.  
  5. Now we know where the Weld is when you equip this tool
  6.  
  7. Each time you equip a this tool it will always make the weld, when you de-equip or unequip the tool it will destroy that weld, we want to adapt to this equip and de-equip/unequip and make a script that replicates it and makes it a Motor6D Now you must be thinking **HOLY SMOLY THIS MUST BE HARD**
  8.  
  9. well not really lets introduce the tool a script by right clicking on the tool and clicking on Script within the Drop down you get
  10.  
  11. Once thats done double click on the script and lets clear out that print("Hello World")
  12.  
  13. So when we clear it the script is Empty, seems lonely and empty lets start with Giving the Tool its own variable Extension
  14.  
  15. ~~~~~~~~~~~~~~~~~
  16. local Tool = script.Parent--[[the script's Parent or wherever its stored under is the Parent so for this script its the Tool and its variable Name extension is given so for now Tool]]--
  17. ~~~~~~~~~~~~~~~~~
  18.  
  19. so when we say `print(Tool.Name)` it will go thru the process of finding what it is and makes it easier to Write neat and clearer code instead of writing script.Parent each time
  20.  
  21. Any way
  22.  
  23. Now that we wrote what Tool is we want to know exactly when the Tool is Equipped
  24.  
  25. which is where..are you ready? **DUN DUN DUN** Events comes into use!
  26.  
  27. With events you can tell exactly when and what gets activated/fired/called! or even deactivated
  28.  
  29. now to know more about events of a tool in general you can visit the [tool instance API/Properties](http://wiki.roblox.com/index.php?title=API:Class/Tool)
  30.  
  31. This will lead to the Roblox Wiki and will give you details about the Tool API!
  32.  
  33. it can explain APIs even better than this answer if you take the time to read and understand it
  34.  
  35. Now lets see about the .events
  36.  
  37. There is the .Equipped(M) event where m is the Mouse
  38.  
  39. `Equipped ( Mouse mouse )`
  40.  
  41. so lets write some code and fill that empty script!
  42.  
  43. so lets make it when ever the event Equipped is fired, we print("Equipped")
  44.  
  45. i'll make a function (which by the way are amazingly useful read about them!)
  46.  
  47. also you don't have to Make the same name but Differentiate it from other functions or make it understandable what it does
  48.  
  49. ~~~~~~~~~~~~~~~~~
  50. function Tool_Equipped(M)----Where M is the Mouse that the Player Uses
  51. print("Equipped")
  52. end
  53. ~~~~~~~~~~~~~~~~~
  54.  
  55. And combine it with the first script and then Connect the function to the event
  56.  
  57. ~~~~~~~~~~~~~~~~~
  58. local Tool = script.Parent
  59.  
  60. function Tool_Equipped(M)
  61. print("Equipped")
  62. end
  63.  
  64. Tool.Equipped:connect(Tool_Equipped)--[[When you write the function make sure it has no brackets inside the already made brackets]]--
  65. ~~~~~~~~~~~~~~~~~
  66.  
  67. by the way Sometimes as explained within the script as you write the Code there might be brackets like `:connect(Tool_Equipped())`
  68.  
  69. Make sure to remove the two brackets so it looks like this `:connect(Tool_Equipped)` not the Outer brackets
  70.  
  71. Okay after that once you play/test the game and put it in your inventory and equip it it will print "Equipped" in Console or if you press F9 you will see it in white text
  72.  
  73. now we have a script that Prints but we want it to change the Weld within Right Arm
  74.  
  75. Now there is many methods where we get the character of the player using the tool
  76.  
  77. Heres my method, this is extremely crucial because you NEED the character to find the Right Arm, the method that is used is that its done by doing this within the Equip Function
  78.  
  79. `plr_char = script.Parent.Parent--[[Meaning that the tool's Parent which when equipped after some delay is within the Character]]--`
  80.  
  81. Here i didn't use local plr_char because we might want to use this in a different event or function
  82.  
  83. when you use local it stays within the function or it can't be used when created previously
  84.  
  85. but when a variable is created without local it can be used outside the function
  86.  
  87. If you wish to know more about Variables click on [this](http://robloxdev.com/articles/Variables)
  88.  
  89. but basically variables work in a sort of scope system wherein local variables are contained within a scope/function/event
  90.  
  91. now lets make a Motor6D within plr_char and then learn how to parent it to the Right Arm
  92.  
  93. so coupled with previous all the other variables, functions and events our script should now look like this!
  94.  
  95. ~~~~~~~~~~~~~~~~~
  96. local Tool = script.Parent
  97.  
  98. function Tool_Equipped(M)
  99. --print("Equipped")--[[two -- makes the line a comment]]--
  100. plr_char = script.Parent.Parent
  101. Right_Arm_Weld = Instance.new("Motor6D")--[[We create the Instance Motor6D]]--
  102. Right_Arm_Weld.Parent = plr_char--[[We then parent it to the Character!]]--
  103.  
  104. end
  105.  
  106. Tool.Equipped:connect(Tool_Equipped)
  107. ~~~~~~~~~~~~~~~~~
  108.  
  109. When we test this it will not print Equipped as we added a Comment line there and then specifies what plr_char variable is and it is script.Parent.Parent which is the Tool's Parent or where its stored Under which when equipped is Parented or Stored in the Character
  110.  
  111. There are other Complicated ways but this is a simple way,
  112.  
  113. but as you can see the Weld is Put into plr_char and not into the Right Arm
  114.  
  115. this is done to test it so far, but now lets jump directly to it in the Arm
  116.  
  117. ~~~~~~~~~~~~~~~~~
  118. local Tool = script.Parent
  119.  
  120. function Tool_Equipped(M)
  121. --print("Equipped")--[[two -- makes the line a comment]]--
  122. plr_char = script.Parent.Parent
  123. Right_Arm = plr_char:FindFirstChild("Right Arm")
  124. Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
  125. Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
  126.  
  127. end
  128.  
  129. Tool.Equipped:connect(Tool_Equipped)
  130. ~~~~~~~~~~~~~~~~~
  131.  
  132. What we did in this Code is made it easier to differentiate which is Right_Arm and Right_Arm_Weld by changing the name of Right_Arm_Weld into Right_Weld
  133.  
  134. and Specified that Right_Arm is the Character's Right Arm we do this by using char:FindFirstChild("Right Arm") which has to be in Quotes as it has Spaces and scripts don't do well with spaces hence why we use Underscores
  135.  
  136. So now the Weld is added in the Right Arm but hold on, its not removing Right Grip
  137.  
  138. woah don't down vote me yet we haven't specified what the Motor6D should be doing at all! we just told it to be Parented to Right Arm and we haven't gotten to Removing Right Grip
  139.  
  140. so lets do that!
  141.  
  142. we use the :Destroy() function which tells the Script to Destroy that Instance
  143.  
  144. so we Find where Right Grip is within Right_Arm variable and :Destroy() it,
  145.  
  146. we do this by Finding "RightGrip" within Right_Arm
  147.  
  148. this is what it looks like in one line
  149.  
  150. `Right_Arm:FindFirstChild("RightGrip"):Destroy()`
  151.  
  152. And this is what it looks like combined so far
  153.  
  154. ~~~~~~~~~~~~~~~~~
  155.  
  156. local Tool = script.Parent
  157.  
  158. function Tool_Equipped(M)
  159. --print("Equipped")--[[two -- makes the line a comment]]--
  160. plr_char = script.Parent.Parent
  161. Right_Arm = plr_char:FindFirstChild("Right Arm")
  162. Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
  163. Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
  164. Right_Arm:FindFirstChild("RightGrip"):Destroy()
  165. end
  166.  
  167. Tool.Equipped:connect(Tool_Equipped)
  168.  
  169. ~~~~~~~~~~~~~~~~~
  170.  
  171. But wait a second why does it error?!
  172.  
  173. well based on how the script runs which is quite a bit faster than the Tool can keep up the RightGrip is probably instanced a Bit slower to fix this we can add a wait or :WaitForChild("RightGrip") on Right_Arm Which will look like this collectively
  174.  
  175. ~~~~~~~~~~~~~~~~~
  176. local Tool = script.Parent
  177.  
  178. function Tool_Equipped(M)
  179. --print("Equipped")--[[two -- makes the line a comment]]--
  180. plr_char = script.Parent.Parent
  181. Right_Arm = plr_char:FindFirstChild("Right Arm")
  182. Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
  183. Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
  184. Right_Arm:WaitForChild("RightGrip",3):Destroy()--[[3 is a given time limit so the script will execute after 3 seconds if it is never found]]--
  185. end
  186.  
  187. Tool.Equipped:connect(Tool_Equipped)
  188. ~~~~~~~~~~~~~~~~~
  189.  
  190. We Removed :FindFirstChild() and put :WaitForChild("RightGrip",3)
  191.  
  192. 3 is the amount of seconds to keep the script asleep or wait, this is done so the script doesn't completely stop forever if it can't find the Weld Now after that you will have successfully removed RightGrip
  193.  
  194. But whats this?! DUN DUN DUN the Handle Falls down, why does it do this?
  195.  
  196. well silly we haven't instructed the script to make Motor6D's Part0 or Part1 the Right Arm and the Handle
  197.  
  198. Basically we never told the script what Motor6D should weld to
  199.  
  200. So before we delete Weld lets add a line where its Part0 and Part1 is Right_Arm and Handle
  201.  
  202. Now the script doesn't know what Handle is, basically the part named Handle inside the Tool
  203.  
  204. or Part that is welded to the right arm
  205.  
  206. Now we don't have to worry about finding the right name if RightGrip is welded as we can Copy Properties!
  207.  
  208. thanks to magic... okay jokes aside we can copy a Value or Point the Value to be copied with
  209.  
  210. lets do that
  211.  
  212. `Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
  213. Right_Weld.Part0 = Right_GripW.Part0
  214. Right_Weld.Part1 = Right_GripW.Part1
  215. `
  216. So what we did was, we Specified where Right_GripW is and then pointed its Part0 and Part1
  217. So the Motor6D copies RightGrip's Part0 and Part1!
  218.  
  219. and now we can Delete Right_GripW
  220.  
  221. Updated Script :
  222.  
  223.  
  224. ~~~~~~~~~~~~~~~~~
  225. local Tool = script.Parent
  226.  
  227. function Tool_Equipped(M)
  228. --print("Equipped")
  229. plr_char = script.Parent.Parent
  230. Right_Arm = plr_char:FindFirstChild("Right Arm")
  231. Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
  232. Right_Weld = Instance.new("Motor6D")
  233. Right_Weld.Part0 = Right_GripW.Part0
  234. Right_Weld.Part1 = Right_GripW.Part1
  235. Right_Weld.Parent = Right_Arm
  236. Right_GripW:Destroy()
  237. end
  238.  
  239. Tool.Equipped:connect(Tool_Equipped)
  240. ~~~~~~~~~~~~~~~~~
  241.  
  242. Now you can see i changed a few things, the very first thing is i placed Right_GripW above Right_Weld is so we can use in within the Weld without Errors, and then i removed the end WaitForChild because we already specified What RightGrip is, and put a Destroy() function to it
  243.  
  244. So now it will copy the Part0 and Part1 and Destroy the Weld
  245.  
  246. This is all you need to do to make the Equip Function work!
  247.  
  248. unless you want to copy the C0 and C1 of that weld which you edited the GripPos etc we will add a edition to that too
  249.  
  250. Right here infact :
  251.  
  252. `
  253. local Tool = script.Parent
  254.  
  255. function Tool_Equipped(M)
  256. --print("Equipped")
  257. plr_char = script.Parent.Parent
  258. Right_Arm = plr_char:FindFirstChild("Right Arm")
  259. Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
  260. Right_Weld = Instance.new("Motor6D")
  261. Right_Weld.Part0 = Right_GripW.Part0
  262. Right_Weld.Part1 = Right_GripW.Part1
  263. Right_Weld.C0 = Right_GripW.C0
  264. Right_Weld.C1 = Right_GripW.C1
  265. Right_Weld.Parent = Right_Arm
  266. Right_GripW:Destroy()
  267. end
  268.  
  269. Tool.Equipped:connect(Tool_Equipped)
  270. `
  271.  
  272. Now lets work on the Un-equip function
  273.  
  274. we want to remove Motor6D, luckily we didn't make it a local variable so we can easily Delete it
  275.  
  276. like this
  277.  
  278. (By the way so far im sorry if you were using the word Deequiping like me the event is actually named Unequipped)
  279. `
  280. function Tool_Unequip()
  281. Right_Weld:Destroy()
  282. print("Unequipped")
  283. end
  284.  
  285. Tool.Unequipped:connect(Tool_Unequip)
  286. `
  287.  
  288. And Update the entire script so far with The C0 and C1 Exceptions too, and a Name Variable c:
  289.  
  290. ~~~~~~~~~~~~~~~~~
  291. local Tool = script.Parent
  292.  
  293. WeldName = "RightMotor6DGrip"---Change the name here if you'd like to
  294.  
  295. function Tool_Equipped(M)
  296. --print("Equipped")
  297. plr_char = script.Parent.Parent
  298. Right_Arm = plr_char:FindFirstChild("Right Arm")
  299. Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
  300. Right_Weld = Instance.new("Motor6D")
  301. Right_Weld.Part0 = Right_GripW.Part0
  302. Right_Weld.Part1 = Right_GripW.Part1
  303. Right_Weld.C0 = Right_GripW.C0
  304. Right_Weld.C1 = Right_GripW.C1
  305. Right_Weld.Parent = Right_Arm
  306. Right_Weld.Name = WeldName
  307. Right_GripW:Destroy()
  308. end
  309.  
  310. function Tool_Unequip()
  311. Right_Weld:Destroy()
  312. print("Unequipped")
  313. end
  314.  
  315.  
  316.  
  317. Tool.Equipped:connect(Tool_Equipped)
  318.  
  319. Tool.Unequipped:connect(Tool_Unequip)
  320. ~~~~~~~~~~~~~~~~~
  321.  
  322. Peace
  323.  
  324.  
  325. Omega_bs
Add Comment
Please, Sign In to add comment