How to make skidding/braking

  1. What do you want to achieve? Keep it simple and clear!
    So, im working on a sonic game on roblox and want to make a skidding/braking system, for example if you’re running forward with W then hold down S you would come to a halt with an animation or if you’re going to the right with D you would hold A to brake, something similar to example
  2. What is the issue? Include screenshots / videos if possible!
    Normally roblox just makes the character turn around if you hold S, seen here
    Screen Recording 2022-04-15 at 1.02.00 PM
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking around the devhub but no posts i’ve seen have fit my needs
    Thanks in advance
2 Likes

oh like in super mario 64 When your going in one direction for a period of time and change he slides?

1 Like

Play an animation when the direction changes for a small amount of time while slowing the velocity of the run, then allow the player to move backward.

yeah i kinda got an idea on WHAT to do but im mostly stuck on HOW to do it

Oh i gotchuu bro, so basically you need to know which key was last pressed so everytime either W,A,S, or D is pressed have a control variable that changes to the last key pressed and then check for a certain amount of time how long the key that isnt the key that was last pressed then do stuff!

If the last key pressed is different to the current key pressed, and last movedirection is the opposite of the current movedirection, anchor the humanoid root part and keep offsetting the position using the old velocity while playing the animation for 2 seconds.

I tried to make thiis as quickly as I could, your probably going to have to change a few things around and maybe add a delay to when the breaks happen but Im giving you the skeleton of it though

No need to use movedirection or anything that complicated

@chumbucketeer

local Players = game:GetService("Players")
local mouse = game.Players.LocalPlayer:GetMouse()

local lastpressed

local Inputs = {
   ["w"] = false,
   ["a"] = false,
   ["s"] = false,
   ["d"] = false
}

mouse.KeyDown:Connect(function(currentKey)
   for i,v in pairs(Inputs) do
      if currentKey == i then
         if lastpressed then
            if lastpressed == "d" and currentKey == "a" then
               print("Break to the right")
            elseif lastpressed == "a" and currentKey =="d" then
               print("Break to the left")
            elseif lastpressed == "s" and currentKey == "w" then
               print("Break to the back")
            elseif lastpressed == "w" and currentKey == "s" then
               print("Break to the front")
            end
         end
         Inputs[i] = true
         lastpressed = i
         break
      end
   end
end)

mouse.KeyUp:Connect(function(key)
   for i,v in pairs(Inputs) do
      if key == i then
         Inputs[i] = false
         break
      end
   end
end)

I didnt really have the patience to make the animation haha

yoyo im not home rn but i’ll def check it out once i am, thanks for the help in advance

1 Like

yeah bro no problem good luck with your project!

1 Like

This is well written, but the only problem is that KeyDown and KeyUp are deprecated by UserInputService. To add to this:

local UserInputService = game:GetService("UserInputService")

local lastpressed

local Inputs = {
   ["w"] = false,
   ["a"] = false,
   ["s"] = false,
   ["d"] = false
}

UserInputService.InputBegan:Connect(function(currentKey)
   for i,v in pairs(Inputs) do
      if currentKey == i then
         if lastpressed then
            if lastpressed == "d" and currentKey == "a" then
               print("Break to the right")
            elseif lastpressed == "a" and currentKey =="d" then
               print("Break to the left")
            elseif lastpressed == "s" and currentKey == "w" then
               print("Break to the back")
            elseif lastpressed == "w" and currentKey == "s" then
               print("Break to the front")
            end
         end
         Inputs[i] = true
         lastpressed = i
         break
      end
   end
end)

UserInputService.InputEnded:Connect(function(key)
   for i,v in pairs(Inputs) do
      if key == i then
         Inputs[i] = false
         break
      end
   end
end)
1 Like

Yeah haha i guess using mouse is more convenient for me

so, with a bit of editing, it works! the current animation im using is a placeholder though, i’ll show a vid of it once i make the actual anim. thanks for the help!

1 Like

Make sure this is done if the humanoid’s magnitude doesn’t get to 0 for too long as this means they are holding both movement keys of their opposites, cancelling them out.