@@ -9,15 +9,16 @@ local RhythmService = {
9
9
-- You can add more tolerance levels if you'd like
10
10
Stopwatch = nil ;
11
11
-- You can use RhythmService.Stopwatch to see if the stopwatch is active
12
+ Sound = {Instance = nil , Keys = {}, KeyPosition = 1 };
12
13
};
13
- local Song = { Sound = nil , Keys = {}, KeyPosition = 1 };
14
+
14
15
local Events = {};
15
16
16
17
function RhythmService :SetSound (sound : Sound , keepKeys : boolean ? , startStopwatchOnPlay : boolean ? )
17
18
-- Set sound and remove keys if necessary
18
- Song .Sound = sound ;
19
+ RhythmService .Sound . Instance = sound ;
19
20
if not keepKeys then
20
- Song .Keys = {};
21
+ RhythmService . Sound .Keys = {};
21
22
end ;
22
23
23
24
if startStopwatchOnPlay then
@@ -30,45 +31,45 @@ function RhythmService:SetSound(sound: Sound, keepKeys: boolean?, startStopwatch
30
31
end ;
31
32
32
33
function RhythmService :AddKey (timePosition : number , index : number ? , endHold : number ? )
33
- assert (Song .Sound , " A sound instance must be defined before adding a key" );
34
+ assert (RhythmService .Sound . Instance , " A sound instance must be defined before adding a key" );
34
35
35
36
-- Add key
36
37
local Key = {timePosition , 1 , endHold or nil };
37
38
if index then
38
- table.insert (Song .Keys , index , Key );
39
+ table.insert (RhythmService . Sound .Keys , index , Key );
39
40
else
40
- table.insert (Song .Keys , Key );
41
+ table.insert (RhythmService . Sound .Keys , Key );
41
42
end ;
42
43
end ;
43
44
44
45
-- function RhythmService:SetKeys(keys: {{number, boolean?}}?)
45
46
function RhythmService :SetKeys (keys )
46
47
-- Set keys
47
- Song .Keys = {};
48
+ RhythmService . Sound .Keys = {};
48
49
if keys then
49
50
for _ , v in ipairs (keys ) do
50
51
if typeof (v ) == " table" then
51
- table.insert (Song .Keys , {v [1 ], 1 , v [2 ]});
52
+ table.insert (RhythmService . Sound .Keys , {v [1 ], 1 , v [2 ]});
52
53
else
53
- table.insert (Song .Keys , {v , 1 });
54
+ table.insert (RhythmService . Sound .Keys , {v , 1 });
54
55
end ;
55
56
end
56
57
end ;
57
58
end ;
58
59
59
60
function RhythmService :RemoveKey (index : number )
60
- table.remove (Song .Keys , index );
61
+ table.remove (RhythmService . Sound .Keys , index );
61
62
end ;
62
63
63
64
function RhythmService :CheckRhythm (noHold : boolean ? )
64
- assert (# Song .Keys > 0 , " There has to be at least one key!" );
65
+ assert (# RhythmService . Sound .Keys > 0 , " There has to be at least one key!" );
65
66
assert (RhythmService .Stopwatch and RhythmService .Stopwatch .Connected , " The stopwatch hasn't started!" );
66
67
67
- local SongPosition = Song .Sound .TimePosition ;
68
- local Goal = Song . Keys [Song .KeyPosition ];
68
+ local SoundPosition = RhythmService .Sound . Instance .TimePosition ;
69
+ local Goal = RhythmService . Sound . Keys [RhythmService . Sound .KeyPosition ];
69
70
local Result = {
70
71
GoalTime = (Goal [2 ] == 0 and noHold and Goal [3 ]) or Goal [1 ];
71
- HitTime = SongPosition ;
72
+ HitTime = SoundPosition ;
72
73
};
73
74
74
75
if (noHold and not Goal [3 ]) or (noHold and Goal [2 ] == 1 ) then
@@ -78,12 +79,12 @@ function RhythmService:CheckRhythm(noHold: boolean?)
78
79
-- Check the time
79
80
for level , tolerance in ipairs (RhythmService .Tolerance ) do
80
81
if Goal [2 ] ~= 0 or (Goal [2 ] == 0 and noHold ) then
81
- if ((noHold and Goal [3 ]) or Goal [1 ]) - tolerance <= SongPosition and SongPosition <= ((noHold and Goal [3 ]) or Goal [1 ]) + tolerance then
82
+ if ((noHold and Goal [3 ]) or Goal [1 ]) - tolerance <= SoundPosition and SoundPosition <= ((noHold and Goal [3 ]) or Goal [1 ]) + tolerance then
82
83
Result .Rating = level ;
83
84
if (noHold and Goal [3 ]) or (not noHold and not Goal [3 ]) then
84
85
RhythmService :ToggleKey (true );
85
86
else
86
- Song . Keys [Song .KeyPosition ][2 ] = 0 ;
87
+ RhythmService . Sound . Keys [RhythmService . Sound .KeyPosition ][2 ] = 0 ;
87
88
end ;
88
89
break ;
89
90
end ;
95
96
96
97
function RhythmService :ToggleKey (disable : boolean ? , index : number ? , keepPosition : boolean ? )
97
98
-- Toggle key and shift position
98
- Song . Keys [Song .KeyPosition or index ][2 ] = (disable and 0 ) or 1 ;
99
- if not keepPosition and # Song . Keys >= Song .KeyPosition + 1 then
100
- Song . KeyPosition = Song .KeyPosition + 1 ;
101
- elseif # Song . Keys < Song .KeyPosition + 2 then
99
+ RhythmService . Sound . Keys [RhythmService . Sound .KeyPosition or index ][2 ] = (disable and 0 ) or 1 ;
100
+ if not keepPosition and # RhythmService . Sound . Keys >= RhythmService . Sound .KeyPosition + 1 then
101
+ RhythmService . Sound . KeyPosition = RhythmService . Sound .KeyPosition + 1 ;
102
+ elseif # RhythmService . Sound . Keys < RhythmService . Sound .KeyPosition + 2 then
102
103
RhythmService :StopStopwatch ();
103
104
end ;
104
105
end ;
105
106
106
107
function RhythmService :ResetKeys ()
107
- for i , key in ipairs (Song .Keys ) do
108
- Song .Keys [i ] = {key [1 ], 1 , key [3 ]};
108
+ for i , key in ipairs (RhythmService . Sound .Keys ) do
109
+ RhythmService . Sound .Keys [i ] = {key [1 ], 1 , key [3 ]};
109
110
end ;
110
111
end ;
111
112
@@ -118,18 +119,18 @@ function RhythmService:StopStopwatch()
118
119
end ;
119
120
120
121
function RhythmService :StartStopwatch ()
121
- assert (Song .Sound , " A sound hasn't been defined!" );
122
- assert (# Song .Keys > 0 , " There has to be at least one key!" );
122
+ assert (RhythmService .Sound . Instance , " A sound hasn't been defined!" );
123
+ assert (# RhythmService . Sound .Keys > 0 , " There has to be at least one key!" );
123
124
124
125
RhythmService :StopStopwatch ();
125
126
RhythmService :ResetKeys ();
126
- Song .KeyPosition = 1 ;
127
+ RhythmService . Sound .KeyPosition = 1 ;
127
128
128
129
-- Add a new SW
129
130
RhythmService .Stopwatch = RunService .Heartbeat :Connect (function ()
130
- for i , v in ipairs (Song .Keys ) do
131
+ for i , v in ipairs (RhythmService . Sound .Keys ) do
131
132
local Limit = (v [2 ] ~= 0 and v [1 ]) or v [3 ]
132
- if Limit and Limit + RhythmService .Tolerance [# RhythmService .Tolerance ] < Song .Sound .TimePosition then
133
+ if Limit and Limit + RhythmService .Tolerance [# RhythmService .Tolerance ] < RhythmService .Sound . Instance .TimePosition then
133
134
RhythmService :ToggleKey (true );
134
135
Events .OnIdle :Fire ();
135
136
break ;
0 commit comments