Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Best way to add a click function for a tool?

Asked by 4 years ago

-Is it mouse.Button1Down and mouse.Button1Up, Enum.UserInputType.MouseButton1, or tool.Activated?

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I would use UserInputService.

To me, I feel like UserInputService is much more flexible than .Activated or any other way you would detect a click with a tool.

Here's how to use it.

01-- Local script inside tool
02local tool = script.Parent
03 
04local UserInputService = game:GetService("UserInputService")
05local enabled = false
06 
07UserInputService.InputBegan:Connect(function(input, gameProcessed)
08    if gameProcessed or not enabled then return end
09    if input.UserInputType == Enum.UserInputType.MouseButton1 then
10        -- Fire remote event
11    end
12end)
13 
14tool.Equipped:Connect(function()
15    enabled = true
16end)
17 
18tool.Unequipped:Connect(function()
19    enabled = false
20end)

Server script:

1RemoteEvent.OnServerEvent:Connect(function(player)
2    -- Code
3end)
0
Nice answer!!! <3 Thank you! :D Inciney 7 — 4y
0
Np! Brandon1881 721 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

depends on what you are trying to do

generally i would go for <mouse.Button1Down and <mouse.Button1Up>

if you want to make a gun for example then best go for the <mouse.Button1Down and <mouse.Button1Up> options

this is mostly opinion

Log in to vote
0
Answered by
DesertusX 435 Moderation Voter
4 years ago

Tool.Activated is the easiest to use for me.

Example of use:

01--LocalScript inside tool
02 
03tool = script.Parent
04 
05function makeEvent()
06    local remoteEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")
07    remoteEvent.Name = PointsEvent
08    remoteEvent:FireServer()
09end
10 
11tool.Activated:connect(makeEvent)
12 
13 
14--ServerScript in ServerScriptService
15 
View all 31 lines...

Hope this helps!

Answer this question