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

Is there any events for a script that can be charged when used by a player?

Hi, some scripts like the knife tool can be charged? How do they do it? I don't recommend to use Button1Down.

1 answer

✓ Accepted answer

I believe you require tick for this problem. For example, if you want to print the amount of time a user has held down the tool for, you can record the tick when they player activates the tool and get the difference in tick when the user releases it. For example, if this was in a script inside the tool:

local Tool = script.Parent
local LastTick = 0

--When the player starts clicking:
Tool.Activated:Connect(function()
	LastTick = tick()
end)

--When the player stops clicking:
Tool.Deactivated:Connect(function()
	
	local ChargeTime = tick() - LastTick
	print("The tool has been charged for " .. ChargeTime)
	
end)

tick() returns a number that constantly increments, increasing by 1 every second & accurate to many decimal places, so when you get the difference in tick from when the tool was first activated to when it is deactivated, you get the amount of time the tool was activated for.

2 comments

0

error to Deactivate? Deactivated?

0

Yeah it should be Deactivated sorry, edited it.

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