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.

-- Local script inside tool
local tool = script.Parent

local UserInputService = game:GetService("UserInputService")
local enabled = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed or not enabled then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- Fire remote event
    end
end)

tool.Equipped:Connect(function()
    enabled = true
end)

tool.Unequipped:Connect(function()
    enabled = false
end)

Server script:

RemoteEvent.OnServerEvent:Connect(function(player)
    -- Code
end)
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:

--LocalScript inside tool

tool = script.Parent

function makeEvent()
    local remoteEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")
    remoteEvent.Name = PointsEvent
    remoteEvent:FireServer()
end

tool.Activated:connect(makeEvent)


--ServerScript in ServerScriptService

local replicatedStorage = game:GetService("ReplicatedStorage")
local eventMade = false
local remoteEvent

if replicatedStorage.PointsEvent then
    remoteEvent = replicatedStorage.PointsEvent
    eventMade = true
end

function addPoints(player)
    player.leaderstats.Points = player.leaderstats.Points + 1
end

if eventMade then
    remoteEvent.OnServerEvent:Connect(addPoints)
end

Hope this helps!

Answer this question