-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpButton.luau
More file actions
182 lines (153 loc) · 5.24 KB
/
Copy pathJumpButton.luau
File metadata and controls
182 lines (153 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
--!strict
--[[
JumpButton by funwolf7
Version 1.0.0
]]
local Players: Players = game:GetService("Players")
local RunService: RunService = game:GetService("RunService")
local LocalPlayer: Player? = Players.LocalPlayer
assert(LocalPlayer, "JumpButton must only be required on the client.")
local defaultBufferDuration: number?
local shouldAutoKillOnJump: boolean = true
local isPressed: boolean = false
local bufferStart: number?
local OnPressBindable: BindableEvent = Instance.new("BindableEvent")
local OnReleaseBindable: BindableEvent = Instance.new("BindableEvent")
RunService:BindToRenderStep("JumpButtonRunner", Enum.RenderPriority.Input.Value + 1, function(): ()
local character: Model? = LocalPlayer.Character
if character then
local humanoid: Humanoid? = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local newState: boolean = humanoid.Jump
if newState ~= isPressed then
isPressed = newState
if newState then
bufferStart = os.clock()
OnPressBindable:Fire()
else
bufferStart = nil
OnReleaseBindable:Fire()
end
end
end
end
end)
local JumpButton = {}
--[[
An RBXScriptSignal that fires whenever the result of `JumpButton.isPressed` changes from false to true.
]]
JumpButton.OnPress = OnPressBindable.Event
--[[
An RBXScriptSignal that fires whenever the result of `JumpButton.isPressed` changes from true to false.
]]
JumpButton.OnRelease = OnReleaseBindable.Event
--[[
Sets the default buffer duration for the library.
The default buffer duration is used for `JumpButton.useIsBuffered` and `JumpButton.peekIsBuffered`.
If newBufferDuration is nil, these functions will not have a maximum time.
]]
function JumpButton.setDefaultBufferDuration(newBufferDuration: number?): ()
defaultBufferDuration = newBufferDuration
end
--[[
Sets if it should automatically kill the buffer when Humanoid.Jumping fires on the local player's humanoid.
Defaults to true.
]]
function JumpButton.setAutoKillOnJump(shouldAutoKill: boolean): ()
shouldAutoKillOnJump = shouldAutoKill
end
--[[
Returns true if the jump button is currently being held down, false otherwise.
]]
function JumpButton.isPressed(): boolean
return isPressed
end
--[[
Sets the current buffer start to nil, effectively preventing code from detecting a buffer.
It will be set to a number when the jump button is pressed again.
]]
function JumpButton.killBuffer(): ()
bufferStart = nil
end
--[[
Returns the current buffer start, if any.
The buffer start is a timestamp from os.clock.
It is not affected by the default buffer duration.
This function will not affect the buffer start.
If you want to automatically kill the buffer, use `JumpButton.useBufferStart`.
]]
function JumpButton.peekBufferStart(): number?
return bufferStart
end
--[[
Returns the current buffer start, if any.
The buffer start is a timestamp from os.clock.
It is not affected by the default buffer duration.
This function will automatically kill the buffer.
If you do not wish to do so, use `JumpButton.peekBufferStart`.
]]
function JumpButton.useBufferStart(): number?
local currentBufferStart: number? = bufferStart
JumpButton.killBuffer()
return currentBufferStart
end
--[[
Returns whether or not there is a buffer, based on the default buffer duration.
Specifically, if the time since the buffer began is less than or equal to the default buffer duration.
If there is no default buffer duration, it will return true if there is a buffer start, false otherwise.
This function will not affect the buffer start.
If you want to automatically kill the buffer, use `JumpButton.useIsBuffered`.
]]
function JumpButton.peekIsBuffered(): boolean
if not bufferStart then
return false
end
if not defaultBufferDuration then
return true
end
return os.clock() - bufferStart <= defaultBufferDuration
end
--[[
Returns whether or not there is a buffer, based on the default buffer duration.
Specifically, if the time since the buffer began is less than or equal to the default buffer duration.
If there is no default buffer duration, it will return true if there is a buffer start, false otherwise.
This function will automatically kill the buffer.
If you do not wish to do so, use `JumpButton.peekIsBuffered`.
]]
function JumpButton.useIsBuffered(): boolean
local isBuffered: boolean = JumpButton.peekIsBuffered()
JumpButton.killBuffer()
return isBuffered
end
do -- Automatically kill buffers on Humanoid.Jumping
local globalConnection: RBXScriptConnection?
local function onCharacterAdded(character: Model?): ()
if not character then
return
end
if globalConnection then
globalConnection:Disconnect()
globalConnection = nil
end
local newConnection: RBXScriptConnection?
newConnection = character.ChildAdded:Connect(function(newChild: Instance)
if newChild:IsA("Humanoid") and newConnection then
newConnection:Disconnect()
if globalConnection == newConnection then
globalConnection = nil
end
newConnection = nil
newChild.Jumping:Connect(function(isActive: boolean)
if isActive and shouldAutoKillOnJump then
JumpButton.killBuffer()
end
end)
end
end)
globalConnection = newConnection
end
LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
onCharacterAdded(LocalPlayer.Character)
end
table.freeze(JumpButton)
return JumpButton