Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **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**
- 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
- Now we know where the Weld is when you equip this tool
- 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**
- 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
- Once thats done double click on the script and lets clear out that print("Hello World")
- So when we clear it the script is Empty, seems lonely and empty lets start with Giving the Tool its own variable Extension
- ~~~~~~~~~~~~~~~~~
- 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]]--
- ~~~~~~~~~~~~~~~~~
- 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
- Any way
- Now that we wrote what Tool is we want to know exactly when the Tool is Equipped
- which is where..are you ready? **DUN DUN DUN** Events comes into use!
- With events you can tell exactly when and what gets activated/fired/called! or even deactivated
- 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)
- This will lead to the Roblox Wiki and will give you details about the Tool API!
- it can explain APIs even better than this answer if you take the time to read and understand it
- Now lets see about the .events
- There is the .Equipped(M) event where m is the Mouse
- `Equipped ( Mouse mouse )`
- so lets write some code and fill that empty script!
- so lets make it when ever the event Equipped is fired, we print("Equipped")
- i'll make a function (which by the way are amazingly useful read about them!)
- also you don't have to Make the same name but Differentiate it from other functions or make it understandable what it does
- ~~~~~~~~~~~~~~~~~
- function Tool_Equipped(M)----Where M is the Mouse that the Player Uses
- print("Equipped")
- end
- ~~~~~~~~~~~~~~~~~
- And combine it with the first script and then Connect the function to the event
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- print("Equipped")
- end
- Tool.Equipped:connect(Tool_Equipped)--[[When you write the function make sure it has no brackets inside the already made brackets]]--
- ~~~~~~~~~~~~~~~~~
- by the way Sometimes as explained within the script as you write the Code there might be brackets like `:connect(Tool_Equipped())`
- Make sure to remove the two brackets so it looks like this `:connect(Tool_Equipped)` not the Outer brackets
- 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
- now we have a script that Prints but we want it to change the Weld within Right Arm
- Now there is many methods where we get the character of the player using the tool
- 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
- `plr_char = script.Parent.Parent--[[Meaning that the tool's Parent which when equipped after some delay is within the Character]]--`
- Here i didn't use local plr_char because we might want to use this in a different event or function
- when you use local it stays within the function or it can't be used when created previously
- but when a variable is created without local it can be used outside the function
- If you wish to know more about Variables click on [this](http://robloxdev.com/articles/Variables)
- but basically variables work in a sort of scope system wherein local variables are contained within a scope/function/event
- now lets make a Motor6D within plr_char and then learn how to parent it to the Right Arm
- so coupled with previous all the other variables, functions and events our script should now look like this!
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")--[[two -- makes the line a comment]]--
- plr_char = script.Parent.Parent
- Right_Arm_Weld = Instance.new("Motor6D")--[[We create the Instance Motor6D]]--
- Right_Arm_Weld.Parent = plr_char--[[We then parent it to the Character!]]--
- end
- Tool.Equipped:connect(Tool_Equipped)
- ~~~~~~~~~~~~~~~~~
- 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
- There are other Complicated ways but this is a simple way,
- but as you can see the Weld is Put into plr_char and not into the Right Arm
- this is done to test it so far, but now lets jump directly to it in the Arm
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")--[[two -- makes the line a comment]]--
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
- Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
- end
- Tool.Equipped:connect(Tool_Equipped)
- ~~~~~~~~~~~~~~~~~
- 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
- 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
- So now the Weld is added in the Right Arm but hold on, its not removing Right Grip
- 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
- so lets do that!
- we use the :Destroy() function which tells the Script to Destroy that Instance
- so we Find where Right Grip is within Right_Arm variable and :Destroy() it,
- we do this by Finding "RightGrip" within Right_Arm
- this is what it looks like in one line
- `Right_Arm:FindFirstChild("RightGrip"):Destroy()`
- And this is what it looks like combined so far
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")--[[two -- makes the line a comment]]--
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
- Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
- Right_Arm:FindFirstChild("RightGrip"):Destroy()
- end
- Tool.Equipped:connect(Tool_Equipped)
- ~~~~~~~~~~~~~~~~~
- But wait a second why does it error?!
- 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
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")--[[two -- makes the line a comment]]--
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_Weld = Instance.new("Motor6D")--[[Renamed To Right_Weld from Right_Arm_Weld]]--
- Right_Weld.Parent = Right_Arm--[[we now parent it to the new variable Right_Arm]]--
- 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]]--
- end
- Tool.Equipped:connect(Tool_Equipped)
- ~~~~~~~~~~~~~~~~~
- We Removed :FindFirstChild() and put :WaitForChild("RightGrip",3)
- 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
- But whats this?! DUN DUN DUN the Handle Falls down, why does it do this?
- well silly we haven't instructed the script to make Motor6D's Part0 or Part1 the Right Arm and the Handle
- Basically we never told the script what Motor6D should weld to
- So before we delete Weld lets add a line where its Part0 and Part1 is Right_Arm and Handle
- Now the script doesn't know what Handle is, basically the part named Handle inside the Tool
- or Part that is welded to the right arm
- Now we don't have to worry about finding the right name if RightGrip is welded as we can Copy Properties!
- thanks to magic... okay jokes aside we can copy a Value or Point the Value to be copied with
- lets do that
- `Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
- Right_Weld.Part0 = Right_GripW.Part0
- Right_Weld.Part1 = Right_GripW.Part1
- `
- So what we did was, we Specified where Right_GripW is and then pointed its Part0 and Part1
- So the Motor6D copies RightGrip's Part0 and Part1!
- and now we can Delete Right_GripW
- Updated Script :
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
- Right_Weld = Instance.new("Motor6D")
- Right_Weld.Part0 = Right_GripW.Part0
- Right_Weld.Part1 = Right_GripW.Part1
- Right_Weld.Parent = Right_Arm
- Right_GripW:Destroy()
- end
- Tool.Equipped:connect(Tool_Equipped)
- ~~~~~~~~~~~~~~~~~
- 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
- So now it will copy the Part0 and Part1 and Destroy the Weld
- This is all you need to do to make the Equip Function work!
- 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
- Right here infact :
- `
- local Tool = script.Parent
- function Tool_Equipped(M)
- --print("Equipped")
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
- Right_Weld = Instance.new("Motor6D")
- Right_Weld.Part0 = Right_GripW.Part0
- Right_Weld.Part1 = Right_GripW.Part1
- Right_Weld.C0 = Right_GripW.C0
- Right_Weld.C1 = Right_GripW.C1
- Right_Weld.Parent = Right_Arm
- Right_GripW:Destroy()
- end
- Tool.Equipped:connect(Tool_Equipped)
- `
- Now lets work on the Un-equip function
- we want to remove Motor6D, luckily we didn't make it a local variable so we can easily Delete it
- like this
- (By the way so far im sorry if you were using the word Deequiping like me the event is actually named Unequipped)
- `
- function Tool_Unequip()
- Right_Weld:Destroy()
- print("Unequipped")
- end
- Tool.Unequipped:connect(Tool_Unequip)
- `
- And Update the entire script so far with The C0 and C1 Exceptions too, and a Name Variable c:
- ~~~~~~~~~~~~~~~~~
- local Tool = script.Parent
- WeldName = "RightMotor6DGrip"---Change the name here if you'd like to
- function Tool_Equipped(M)
- --print("Equipped")
- plr_char = script.Parent.Parent
- Right_Arm = plr_char:FindFirstChild("Right Arm")
- Right_GripW = Right_Arm:WaitForChild("RightGrip",2)
- Right_Weld = Instance.new("Motor6D")
- Right_Weld.Part0 = Right_GripW.Part0
- Right_Weld.Part1 = Right_GripW.Part1
- Right_Weld.C0 = Right_GripW.C0
- Right_Weld.C1 = Right_GripW.C1
- Right_Weld.Parent = Right_Arm
- Right_Weld.Name = WeldName
- Right_GripW:Destroy()
- end
- function Tool_Unequip()
- Right_Weld:Destroy()
- print("Unequipped")
- end
- Tool.Equipped:connect(Tool_Equipped)
- Tool.Unequipped:connect(Tool_Unequip)
- ~~~~~~~~~~~~~~~~~
- Peace
- Omega_bs
Add Comment
Please, Sign In to add comment