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

How do I detect if a player is hold-clicking a tool?

Hello,

I have a tool that gives you cash when you click, but how do I make it so that players can hold-click the tool to get cash (just like in simulator games)?

Here is my script:

local tool = script.Parent
local Handle = tool.Handle

local CashValue = tool:WaitForChild("CashValue").Value

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Remotes:WaitForChild("GiveCashEvent")

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local ShakeAnim = Humanoid:LoadAnimation(script.Parent:WaitForChild("ShakeAnim"))
local debounce = false

tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		ShakeAnim:Play()
		Event:FireServer(CashValue)
		wait(0.6)
		debounce = false
	end
end)

tool.Unequipped:Connect(function()
	ShakeAnim:Stop()
end)

Thank you for your time!

2 comments

0

put the cash value in the normal script, exploiters can just change the cashvalue to something like math.huge and get infinite coins

0

Cash value is how much money a player gets with each click.

2 answers

Using UserInputService is an easy method for press-and-hold events.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end -- game processed event checks if the player is talking or is in the menu and if he is then it doesn't run the function

	if Input.UserInputType == Enum.UserInputType.MouseButton1 then

	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end

	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		-- stop doing stuff
	end
end)

You'll have to fit this into your code, and I'm assuming you can. But if you can't, comment and I'll see if I can give pointers.

1 comment

0

The problem is that this is only the Activated event. He wants the button held down.

The problem with the tool events is that it only 'simulates' everything. To make something more complex, you should use UserInputService. UserInputService checks for input made by the client. Of course, since this is the client, you have to use a localscript.

local tool = script.Parent
local Handle = tool.Handle

local CashValue = tool:WaitForChild("CashValue").Value

local UIS = game:GetService("UserInputService") --This is the service you need.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Remotes:WaitForChild("GiveCashEvent")

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local ShakeAnim = Humanoid:LoadAnimation(script.Parent:WaitForChild("ShakeAnim"))
local debounce = false

script.Parent.Equipped:Connect(function() --Make sure this only activates when the tool is equipped.
	UIS.InputBegan:Connect(function(input, GPE)
		if GPE then return end --This makes sure it does nothing when e.g. chatting.
		if not debounce then
			debounce = true
			if not UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then --This checks if the mouse is being held down.
				return --If this is any other input, then do nothing.
			else --If it is the MouseButton1 (Left Click)
				ShakeAnim:Play()
				Event:FireServer(CashValue)
			end
			wait(0.6)
			debounce = false
		end
	end)
end)

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