Hello! I have made a working Sonic acceleration script, and it works fine. It’s just if I stop moving while it’s waiting and then start moving again, I skip a stage. This is probably very simple to fix but my smooth brain can’t figure it out. Here is the code:
local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
--CONFIG
local stage1Speed = 12
local stage2Speed = 40
local stage3Speed = 77
local stage4Speed = 100 --This speed is unreachable unless the player hits a spring.
local stage1Length = 2
local stage2Length = 4
--No stage 3 length because stage 4 is unreachable.
currentStage = 1
humanoid.Changed:Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if currentStage == 1 then
wait(stage1Length)
if humanoid.MoveDirection.Magnitude > 0 then
currentStage = 2
humanoid.WalkSpeed = stage2Speed
end
elseif currentStage == 2 then
wait(stage2Length)
if humanoid.MoveDirection.Magnitude > 0 then
currentStage = 3
humanoid.WalkSpeed = stage3Speed
end
end
else
currentStage = 1
humanoid.WalkSpeed = stage1Speed
end
end)
local Tween = game:GetService("TweenService"):Create(Humanoid, TweenInfo.new(6), {WalkSpeed = 100})
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
Tween:Play()
else
Tween:Cancel()
end
end)
The problem is probably the wait(stagelength). When you are gaining speed and you are in stage 2, suddenly stopping will not stop the wait functions. So when you stop faster before the wait ends. It will set your stage to currentStage = 3.
One way you could fix this is just add a cool down before players can run faster again after stopping.
His problem, actually, is not the not-smoothly changing of speed. His problem is that when you stop too quick and run again, the speed skips a stage so instead of Stage1 → Stage2, it would be Stage1 → Stage 3. I don’t think tweening would solve the problem.