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

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

Asked by 5 years ago

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

1 answer

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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.

0
error to Deactivate? Deactivated? cherrythetree 130 — 5y
0
Yeah it should be Deactivated sorry, edited it. mattscy 3725 — 5y
Ad

Answer this question