Help to sprint fix

Hello everyone, I created a script so that when you run, you get 0.4 speed, and so I decided to create, I also have a sprint button, when you press, you have a speed of 16, but this does not happen, more precisely, as I start running, my speed increases and I run again to the speed that I had, and I want the speed to be 16 when pressing, and when you press again, the speed that was returned
Screenshot_20231020_101629

That is, look, if it runs, your speed is increased by 0.4 and you have already gained 36 speed, and you press the button and your speed becomes 16, but this does not happen for me, when running, it still returns on the screenshot, I showed a script that gives 0.4 speed

I’m not sure what you mean i can’t quite understand it but i think you could try something like

local player=game.players.localplayer
local character=script.parent
local humanoid=character:WaitForChild("Humanoid")

local sprint=false

local function sprint() --Just hook this function to how you toggle the sprint
 if sprint==false then --player wasn't sprinting
   sprint=true --Change sprint bool for the next toggle
   local speed=player.Agility
   if speed.value/4 >= 16 then
        local walkspeed=speed.value/4
       humanoid.WalkSpeed=walkspeed
   end
 else --If player was already sprinting
    sprint=false
    humanoid.WalkSpeed=16 --or whatever you want the default walkspeed to be
  end
end

Look, I’m trying when I use the button, my speed gets to 16 and then when I start running, I get it back, help me make it so that when I press the buttons, it returns, not when I run

What do you mean by return do you mean something like


local function Add(a,b)
  return a+b
end

Local result=Add(3,6) --equals 9

It’s not very clear what you mean, I’m sorry

Sorry for the translator, I mean, you run and press the button, you have a speed of 16, but this doesn’t happen, I start running, my speed returns, help me make it so that when you press the button again, the speed returns, and not when you run,

Okay i see now the script i said earlier should have worked or at least worked as a base to adapt though, anyways you should set the running speed only affect when players are running and track whether they are with a variable or if you are using UserInputService you can make their speed go back to 16 with the .InputEnded event