Speed Issue (don't know what to call it) [SOLVED]

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")

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

	if humanoid.MoveDirection.Magnitude > 0 then
		if currentStage == 1 then
			if humanoid.MoveDirection.Magnitude > 0 then
				currentStage = 2
				humanoid.WalkSpeed = stage2Speed
		elseif currentStage == 2 then
			if humanoid.MoveDirection.Magnitude > 0 then
				currentStage = 3
				humanoid.WalkSpeed = stage3Speed
		currentStage = 1
		humanoid.WalkSpeed = stage1Speed

If I recall, sonic slowly builds up speed when he’s sprinting (I only played the 3d games), you can achieve the same with tweenservice.

Yes, Sonic slowly builds up speed. You’re saying to tween the WalkSpeed?

Yes and you can cancel it when the player stops moving. I recommend using .Running connection instead of .Changed btw.

Alright, I’ll try it your suggestions.

local Tween = game:GetService("TweenService"):Create(Humanoid, TweenInfo.new(6), {WalkSpeed = 100})
   if Speed > 0 then

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.

I changed Tween:Cancel() to Tween:Stop() because if not, the player never goes back to the first speed.

Ah okay, It was an example snippet anyway, so did it work and was it what you were looking for originally?

I just realized “Stop()” does not exist on tweens. Cancel did work, I just had to set the speed back to 12. Thank you!

Stop() is just Cancel()
Anyway, you can mark it as a solution so this thread closes.

Yeah, now I just have to edit the length of the tween to make it last longer.

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.

Tweening did solve it and sonic smoothly changes speed. So it would make sense at least.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.