Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Using LocalScripts instead of Tools/HopperBins
- -- By EmeraldSlash, 16/11/15
- --[[
- Hello! Do you like creating HopperBins/Tools with functions like 'KeyDown' and 'Button1Down'?
- 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!
- Here is the hierarchy of the following example:
- Workspace
- StarterGui
- etc.
- StarterPack
- HopperBin
- Script
- LocalScript
- This is the script inside the HopperBin:
- vvvvvvvvvvvvvvvvvvvvvvvvvvv
- local tool = script.Parent
- local char = tool.Parent
- function gen(m)
- local part = Instance.new("Part",game.Workspace)
- local p = game.Players:GetPlayerFromCharacter(char)
- part.Name = p.Name.. "Part"
- part.CFrame = CFrame.new(m.Hit + Vector3.new(0,2,0),char.Head.Position)
- part.Size = Vector3.new(3,3,3)
- part.BrickColor = p.TeamColor
- part.Transparency = 0.5
- print("Generating part.")
- end
- function delete()
- local part = game.Workspace:FindFirstChild(char.Name.. "Part")
- if part then
- part:Destroy()
- else
- print("Part not found.")
- end
- end
- local using = false
- tool.Selected:connect(function(mouse)
- mouse.Button1Down:connect(function()
- if using == false then
- using = true
- gen(mouse.Hit)
- end
- end)
- mouse.Button2Down:connect(function()
- if using == true then
- delete()
- wait(5)
- using = false
- end
- end)
- end)
- ^^^^^^^^^^^^^^^^^^^^^^^^^^
- This script will generate a part where the player clicks, the part facing the player. When the player right clicks, the part removes itself.
- Now I will change this to a LocalSript:
- vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- local p = game.Players.LocalPLayer
- repeat wait() until p.Character
- local char = p.Character
- local mouse = p:GetMouse
- function gen(m)
- local part = Instance.new("Part",game.Workspace)
- part.Name = p.Name.. "Part"
- part.CFrame = CFrame.new(m.Hit + Vector3.new(0,2,0),char.Head.Position)
- part.Size = Vector3.new(3,3,3)
- part.BrickColor = p.TeamColor
- part.Transparency = 0.5
- print("Generating part.")
- end
- function delete()
- local part = game.Workspace:FindFirstChild(char.Name.. "Part")
- if part then
- part:Destroy()
- else
- print("Part not found.")
- end
- end
- local using = false
- mouse.Button1Down:connect(function()
- if using == false then
- using = true
- gen(mouse.Hit)
- end
- end)
- mouse.Button2Down:connect(function()
- if using == true then
- delete()
- wait(5)
- using = false
- end
- end)
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- As you can see, there are very few actual changes. The biggest changes are:
- - The variables are defined differently at the start
- - LocalScripts can not be 'selected' or 'deselected', so I just forgot that. You can program that, though.
- The following is all in a LocalScript.
- This is how you detect keys:
- Version 1 (this form of key detection is deprecated):
- http://wiki.roblox.com/index.php?title=KeyDown
- vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- local p = game.Players.LocalPlayer
- local mouse = p:GetMouse()
- mouse.KeyDown:connect(function(key)
- key = key:lower() -- so the key can be lowercase
- if key == "f" then
- print("'F' was pressed!")
- end
- end)
- ^^^^^^^^^^^^^^^^^^^^^^^^
- Version 2 (a lot better, cross platform):
- http://wiki.roblox.com/index.php?title=API:Class/ContextActionService
- http://wiki.roblox.com/index.php?title=ContextActionService_Tutorial
- vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- local context = game:GetService("ContextActionService")
- function doStuff()
- print("'F' was pressed!")
- end
- context:BindAction("ActionName",doStuff,false,"f")
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- To explain about this service:
- :BindAction:
- argument 1 - the name of your action
- argument 2 - function that you will call, do not put '()' at the end
- argument 3 - whether it will create a button on a mobile device
- argument 4 - the key that you press to activate it
- :UnbindAction:
- argument 1 - the name of the action that you want to unbind
- That's the basics of ContextActionService. If you need an example of a comparison between LocalScript and HopperBin/Tool, PM me on ROBLOX.
- Hope I helped :)
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement