Skip to content

Commit 6025525

Browse files
Update RhythmService.lua
1 parent 39d116e commit 6025525

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

RhythmService.lua

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ local RhythmService = {
99
-- You can add more tolerance levels if you'd like
1010
Stopwatch = nil;
1111
-- You can use RhythmService.Stopwatch to see if the stopwatch is active
12+
Sound = {Instance = nil, Keys = {}, KeyPosition = 1};
1213
};
13-
local Song = {Sound = nil, Keys = {}, KeyPosition = 1};
14+
1415
local Events = {};
1516

1617
function RhythmService:SetSound(sound: Sound, keepKeys: boolean?, startStopwatchOnPlay: boolean?)
1718
-- Set sound and remove keys if necessary
18-
Song.Sound = sound;
19+
RhythmService.Sound.Instance = sound;
1920
if not keepKeys then
20-
Song.Keys = {};
21+
RhythmService.Sound.Keys = {};
2122
end;
2223

2324
if startStopwatchOnPlay then
@@ -30,45 +31,45 @@ function RhythmService:SetSound(sound: Sound, keepKeys: boolean?, startStopwatch
3031
end;
3132

3233
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");
3435

3536
-- Add key
3637
local Key = {timePosition, 1, endHold or nil};
3738
if index then
38-
table.insert(Song.Keys, index, Key);
39+
table.insert(RhythmService.Sound.Keys, index, Key);
3940
else
40-
table.insert(Song.Keys, Key);
41+
table.insert(RhythmService.Sound.Keys, Key);
4142
end;
4243
end;
4344

4445
-- function RhythmService:SetKeys(keys: {{number, boolean?}}?)
4546
function RhythmService:SetKeys(keys)
4647
-- Set keys
47-
Song.Keys = {};
48+
RhythmService.Sound.Keys = {};
4849
if keys then
4950
for _, v in ipairs(keys) do
5051
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]});
5253
else
53-
table.insert(Song.Keys, {v, 1});
54+
table.insert(RhythmService.Sound.Keys, {v, 1});
5455
end;
5556
end
5657
end;
5758
end;
5859

5960
function RhythmService:RemoveKey(index: number)
60-
table.remove(Song.Keys, index);
61+
table.remove(RhythmService.Sound.Keys, index);
6162
end;
6263

6364
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!");
6566
assert(RhythmService.Stopwatch and RhythmService.Stopwatch.Connected, "The stopwatch hasn't started!");
6667

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];
6970
local Result = {
7071
GoalTime = (Goal[2] == 0 and noHold and Goal[3]) or Goal[1];
71-
HitTime = SongPosition;
72+
HitTime = SoundPosition;
7273
};
7374

7475
if (noHold and not Goal[3]) or (noHold and Goal[2] == 1) then
@@ -78,12 +79,12 @@ function RhythmService:CheckRhythm(noHold: boolean?)
7879
-- Check the time
7980
for level, tolerance in ipairs(RhythmService.Tolerance) do
8081
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
8283
Result.Rating = level;
8384
if (noHold and Goal[3]) or (not noHold and not Goal[3]) then
8485
RhythmService:ToggleKey(true);
8586
else
86-
Song.Keys[Song.KeyPosition][2] = 0;
87+
RhythmService.Sound.Keys[RhythmService.Sound.KeyPosition][2] = 0;
8788
end;
8889
break;
8990
end;
@@ -95,17 +96,17 @@ end;
9596

9697
function RhythmService:ToggleKey(disable: boolean?, index: number?, keepPosition: boolean?)
9798
-- 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
102103
RhythmService:StopStopwatch();
103104
end;
104105
end;
105106

106107
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]};
109110
end;
110111
end;
111112

@@ -118,18 +119,18 @@ function RhythmService:StopStopwatch()
118119
end;
119120

120121
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!");
123124

124125
RhythmService:StopStopwatch();
125126
RhythmService:ResetKeys();
126-
Song.KeyPosition = 1;
127+
RhythmService.Sound.KeyPosition = 1;
127128

128129
-- Add a new SW
129130
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
131132
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
133134
RhythmService:ToggleKey(true);
134135
Events.OnIdle:Fire();
135136
break;

0 commit comments

Comments
 (0)