Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Best way to add a click function for a tool?

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

3 answers

✓ Accepted answer

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)

2 comments

0

Nice answer!!! <3 Thank you! :D

0

Np!

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

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.