Trying to write a WalkSpeed slowdown

I am trying to write a script that once the player’s WalkSpeed increases it waits a bit for a few seconds and then decreases the speed. However it seems as if I can’t figure out a way to propperly cancel it if it gets sped up a second time or if the player suddenly stopped (there is another connection that will automatically set the player’s WalkSpeed to the default StarterPlayer.CharacterWalkSpeed).

I also have it set so that when the player is in the air the function will pause until it’s on the ground.

This code when run will simply cancel itself out allowing the player to stay at it’s sped up speed indefinably. I’ve been trying to debug this for a while and can’t figure out a good way to solve this. Thanks.

local setSpeed = game.StarterPlayer.CharacterWalkSpeed
local TBActivated = 0

local function walkspeedCheck(currentSpeed)
	
	if currentSpeed >= setSpeed and TBActivated <= 0 then

		setSpeed = currentSpeed

	else

		--print("SpeedUp Cooldown Canceled")
		if currentSpeed ~= game.StarterPlayer.CharacterWalkSpeed  then
			hum.WalkSpeed = setSpeed
		end
		return "end"

	end
	
end

local function TempBoost()
	
	wait(0.2)
	
	local currentSpeed = hum.WalkSpeed
	
	if walkspeedCheck(currentSpeed) == "end" then print("hfyaseuibfgyawer") return end
	
	TBActivated += 1
	
	local cooldown = 3
	
	while cooldown > 0.1 do
		
		while hum.FloorMaterial == Enum.Material.Air do print("hfyaseuibfgyawer12") task.wait() end
		
		if hum.WalkSpeed == game.StarterPlayer.CharacterWalkSpeed or walkspeedCheck(currentSpeed) == "end" then print("hfyaseuibfgyawer1") break end
		
		cooldown -= 0.1
		wait(0.1)
		
	end
	
	if hum.WalkSpeed == game.StarterPlayer.CharacterWalkSpeed or walkspeedCheck(currentSpeed) == "end" then TBActivated -= 1 return end

	while hum.WalkSpeed > game.StarterPlayer.CharacterWalkSpeed do

		while hum.FloorMaterial == Enum.Material.Air do print("hfyaseuibfgyawer13") task.wait() end
		
		if hum.WalkSpeed == game.StarterPlayer.CharacterWalkSpeed or walkspeedCheck(currentSpeed) == "end" then TBActivated -= 1 print("hfyaseuibfgyawer2") break end
		
		script.Parent.Humanoid.WalkSpeed -= 1
		wait(0.1)
		
	end
	
	--if walkspeedCheck(currentSpeed) == "end" then return end
	
	if walkspeedCheck(currentSpeed) ~= "end" then
		script.Parent.Humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
		setSpeed = game.StarterPlayer.CharacterWalkSpeed
	end
	
	TBActivated -= 1
	
end
hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	
	if hum.MoveDirection:Dot(hum.MoveDirection) == 0 then
		
		hum.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
		
	end
	
end)


hum:GetPropertyChangedSignal("WalkSpeed"):Connect(TempBoost) -- Connection

after the wait maybe you can check if the player is still or it’s being sped up another time?

That is what’s already happening, with the “MoveDirection” Connect setting the Humanoid WalkSpeed to The Game’s default, and the first function mentioned at the top respectively for checking if the player’s newly set walkspeed is higher than what was perviously being set.