My slowdown system when you run is instable!

Hello I’m Ryxku and I’m trying to make a slow down system like in Sonic games by example.
In many video games when our character is running fast and we want to stop it, we release the button we were using. In roblox, the slowdown is very slight! VERY SLIGHT! And there’s no way to edit it! So for my racing game, players run with walkspeed of 50 in average, and we want to stop running and go back to idle, i wanted to make something realistic, so I made an animation for Slowdown but it rarely works, the script is stored in ServerScriptService. Anybody knows why my script is unstable?

local ms = script:WaitForChild("MultiSettingsMode",30).Value
local dtsi = 0
local ddsi = 0

task.wait()

if ms then
	dtsi = 200-195*10^(-0.01*script.MultiSettings.DecelerationTimeSpeedInfluence.Value)
	ddsi = script.MultiSettings.DecelerationDistanceSpeedInfluence.Value /25
else 
	dtsi = 200-195*10^(-0.01*script.MultiSettings.DecelerationTimeSpeedInfluence.Value)
	ddsi = script.MonoSetting.SpeedInfluence.Value / 25
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			print(char.Humanoid.MoveDirection.Magnitude)
			if char.Humanoid.MoveDirection.Magnitude <= 0 and plr:GetAttribute("InGame") and plr:GetAttribute("InGame") == true and char.Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall and char.Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
				print("eeeeeeeeeeeeee")
				local ap = Instance.new("AlignPosition",char.HumanoidRootPart)
				ap.Mode = Enum.PositionAlignmentMode.OneAttachment
				local at = Instance.new("Attachment",char.HumanoidRootPart)
				ap.Attachment0 = at
				local pos = (char.HumanoidRootPart.CFrame * CFrame.new(0,0,-(char.Humanoid.WalkSpeed*ddsi))).Position
				ap.Position = pos
				ap.Responsiveness = dtsi
				local secucount = 0
				local anim = nil
				if plr:GetAttribute("AnimationPack") then
					if game:GetService("ServerStorage").AnimationPacks[plr:GetAttribute("AnimationPack")]:FindFirstChild("Frein") then
						anim = char.Humanoid:LoadAnimation(game:GetService("ServerStorage").AnimationPacks[plr:GetAttribute("AnimationPack")]:FindFirstChild("Frein"))
						anim:Play()
					end
				elseif plr:GetAttribute("Morph") then
					if game:GetService("ServerStorage").AnimationPacks:FindFirstChild(tostring(plr:GetAttribute("Morph")).."A") and game:GetService("ServerStorage").AnimationPacks[tostring(plr:GetAttribute("Morph")).."A"]:FindFirstChild("Frein") then
						anim = char.Humanoid:LoadAnimation(game:GetService("ServerStorage").AnimationPacks[tostring(plr:GetAttribute("Morph")).."A"]:FindFirstChild("Frein"))
						anim:Play()
					end
				end
				repeat
					task.wait()
					secucount += 1
				until pos.Magnitude - char.HumanoidRootPart.Position.Magnitude <= 1.1 or secucount >= 500
				ap:Destroy()
				at:Destroy()
				if anim then
					anim:Stop()
				end
			end
		end)
	end)
end)

Here two videos to show you how the slow down is working weird


For this last video, it’s the only moment where my system works perfectly

If someone can find a solution, I would be very happy :slight_smile: !

Every time the move direction updates (every heartbeat), this loop runs run. Since it doesn’t stop until 8 seconds have passed, this means that about 480 loops can stack on top of each other.

Yes I noticed that recently, but I have no idea how to fix it. I don’t know if it’s because my script does too many tasks at once…

I could help you more, but I don’t know the full extent of the script to help you exactly, nor do I know the other variables in play.

So in my racing game, there’s a lobby and maps, the script starts to work when I’m in a map, because my walkspeed is different in a lobby. In a map, my walkspeed is very high.
Also the rest I use for my script :
image