Advertisement
EmeraldSlash

Using LocalScripts instead of Tools [ROBLOX]

Nov 15th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Using LocalScripts instead of Tools/HopperBins
  2. -- By EmeraldSlash, 16/11/15
  3.  
  4. --[[
  5. Hello! Do you like creating HopperBins/Tools with functions like 'KeyDown' and 'Button1Down'?
  6. I find that tools look unprofessional, and can be annoying at times. To fix this, I am making a guide on creating the same abilities - except with LocalScripts!
  7.  
  8. Here is the hierarchy of the following example:
  9.  
  10. Workspace
  11. StarterGui
  12. etc.
  13. StarterPack
  14.     HopperBin
  15.         Script
  16.     LocalScript
  17.  
  18. This is the script inside the HopperBin:
  19.  
  20. vvvvvvvvvvvvvvvvvvvvvvvvvvv
  21. local tool = script.Parent
  22. local char = tool.Parent
  23.  
  24. function gen(m)
  25.     local part = Instance.new("Part",game.Workspace)
  26.     local p = game.Players:GetPlayerFromCharacter(char)
  27.     part.Name = p.Name.. "Part"
  28.     part.CFrame = CFrame.new(m.Hit + Vector3.new(0,2,0),char.Head.Position)
  29.     part.Size = Vector3.new(3,3,3)
  30.     part.BrickColor = p.TeamColor
  31.     part.Transparency = 0.5
  32.     print("Generating part.")
  33. end
  34.  
  35. function delete()
  36.     local part = game.Workspace:FindFirstChild(char.Name.. "Part")
  37.     if part then
  38.         part:Destroy()
  39.     else
  40.         print("Part not found.")
  41.     end
  42. end
  43.  
  44. local using = false
  45.  
  46. tool.Selected:connect(function(mouse)
  47.     mouse.Button1Down:connect(function()
  48.         if using == false then
  49.             using = true
  50.             gen(mouse.Hit)
  51.         end
  52.     end)
  53.     mouse.Button2Down:connect(function()
  54.         if using == true then
  55.             delete()
  56.             wait(5)
  57.             using = false
  58.         end
  59.     end)
  60. end)
  61. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  62.  
  63. This script will generate a part where the player clicks, the part facing the player. When the player right clicks, the part removes itself.
  64. Now I will change this to a LocalSript:
  65.  
  66. vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  67. local p = game.Players.LocalPLayer
  68. repeat wait() until p.Character
  69. local char = p.Character
  70. local mouse = p:GetMouse
  71.  
  72. function gen(m)
  73.     local part = Instance.new("Part",game.Workspace)
  74.     part.Name = p.Name.. "Part"
  75.     part.CFrame = CFrame.new(m.Hit + Vector3.new(0,2,0),char.Head.Position)
  76.     part.Size = Vector3.new(3,3,3)
  77.     part.BrickColor = p.TeamColor
  78.     part.Transparency = 0.5
  79.     print("Generating part.")
  80. end
  81.  
  82. function delete()
  83.     local part = game.Workspace:FindFirstChild(char.Name.. "Part")
  84.     if part then
  85.         part:Destroy()
  86.     else
  87.         print("Part not found.")
  88.     end
  89. end
  90.  
  91. local using = false
  92.  
  93. mouse.Button1Down:connect(function()
  94.     if using == false then
  95.         using = true
  96.         gen(mouse.Hit)
  97.     end
  98. end)
  99.  
  100. mouse.Button2Down:connect(function()
  101.     if using == true then
  102.         delete()
  103.         wait(5)
  104.         using = false
  105.     end
  106. end)
  107. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  108.  
  109. As you can see, there are very few actual changes. The biggest changes are:
  110. - The variables are defined differently at the start
  111. - LocalScripts can not be 'selected' or 'deselected', so I just forgot that. You can program that, though.
  112.  
  113. The following is all in a LocalScript.
  114.  
  115. This is how you detect keys:
  116.  
  117. Version 1 (this form of key detection is deprecated):
  118.  
  119. http://wiki.roblox.com/index.php?title=KeyDown
  120.  
  121. vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  122. local p = game.Players.LocalPlayer
  123. local mouse = p:GetMouse()
  124.  
  125. mouse.KeyDown:connect(function(key)
  126.     key = key:lower() -- so the key can be lowercase
  127.     if key == "f" then
  128.         print("'F' was pressed!")
  129.     end
  130. end)
  131. ^^^^^^^^^^^^^^^^^^^^^^^^
  132. Version 2 (a lot better, cross platform):
  133.  
  134. http://wiki.roblox.com/index.php?title=API:Class/ContextActionService
  135. http://wiki.roblox.com/index.php?title=ContextActionService_Tutorial
  136.  
  137. vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  138. local context = game:GetService("ContextActionService")
  139.  
  140. function doStuff()
  141.     print("'F' was pressed!")
  142. end
  143.  
  144. context:BindAction("ActionName",doStuff,false,"f")
  145. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  146. To explain about this service:
  147.  
  148. :BindAction:
  149.     argument 1 - the name of your action
  150.     argument 2 - function that you will call, do not put '()' at the end
  151.     argument 3 - whether it will create a button on a mobile device
  152.     argument 4 - the key that you press to activate it
  153. :UnbindAction:
  154.     argument 1 - the name of the action that you want to unbind
  155.  
  156. That's the basics of ContextActionService. If you need an example of a comparison between LocalScript and HopperBin/Tool, PM me on ROBLOX.
  157.  
  158. Hope I helped :)
  159. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement