Whats the best way create a Dashing script

I want to make a dashing script that When you Press WW, or AA, or DD for example the movement keys twice you’d dash in those directions. Dont have an animation yt I just went them to move for now. Any help appreciated.

13 Likes

Userinputservice or context action service is what u can use

Yea I know about that stuff. I just dont know How I’d actually get the player to move.

You can get the player to move by instancing a “BodyVelocity” into their character. I most people put it in the HumanoidRootPart. Then changing the MaxForce and Velocity. The direction is done by the Velocity, which you will need to calculate.

Yup im trying that tell me if you think its going good so far, what should I change.
Also how would I have it respond to me pressing W twice, and not just once.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UIP = game:GetService("UserInputService")



local Debounce = false

UIP.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end 
	if Input.KeyCode == Enum.KeyCode.w then
		   if not Debounce then Debounce = true
			wait(1)
			Debounce = false
		else
			Character.HumanoidRootPart.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 50
		end
	end
end)

In order to get them to move, I’d highly recommend using BodyPosition. Compute the position where you want the player to go by multiplying the HumanoidRootPart’s CFrame X or Z component for either a forward/backward or sideward dash, respectively.

I used to use BodyVelocity and experimented with other BodyMovers, but BodyVelocity will make the dash longer if the player is in mid-air because of friction. With BodyPosition, you get more consistent results, regardless of if the player is in motion/mid-air/grounded.

15 Likes

I’m fairly new to scripting so what you described above is a bit complicated for me, Mind helping me out with that?

You could use tick() to respond to pressing W twice.
Example:

  • One tick() as a variable outside the function
    local var = tick()
  • Another one checking like this Input.KeyCode == Enum.KeyCode.w and tick() - var < 0.175 I find 0.175 somewhat of a good intermission.
1 Like

Use a value that is set to true upon clicking and then after 1 second set it to false if the variable is true when clicking then the plsyer had already clicked the button before and by that you can detect double click. Then just make it’s walk speed higher and then set it back to normal or make a function that moves the player to a certain direction in relation to the characters heading and then call the function upon double click

1 Like

Nothing happens, I dont get it lol. according to what I searched online Tick() checks time in second between it and something?

Here’s something I found that could help you out with that I guess: Link

1 Like

05/23/22: I’ve been getting a number of questions regarding the dashing script, and I’ve made an up-to-date tutorial here:
(it has better code practices and uses non-deprecated lua functions)


Of course. It’s what the DevForum is here for.

Let’s say that you want the player to dash 20 units away from them.

local Dash_Normal = 20 -- set the default range for better control if you want to change it later on
local Dash_Timeout = 0.15 -- how long will it wait in-between taps; think of this as tap speed
local Can_Dash = false -- can the player dash immediately? no.

Your current debounce system is fine, however I would put the wait() function in a coroutine so the entire script doesn’t yield.

local UIS = game:GetService('UserInputService')
UIS.InputBegan:Connect(function(input,istyping)
    if istyping then return end
    if input.KeyCode == Enum.KeyCode.W then
        if Can_Dash == true then
            -- do the dash hokey-pokey
        else
            Can_Dash = true
            coroutine.wrap(function()
                wait(Dash_Timeout)
                Can_Dash = false
            end)()
        end
    end
end)

Now we have our basic input detection established. Usually people will put the entire dash function inside of the InputBegan() function, but as your scripts grow more and more complex, it would be easier to call a function that does everything for you.

function DashForward(root)
    local i = Instance.new('BodyPosition')
    i.MaxForce = Vector3.new(1000000,0,1000000) -- y-component is 0 because we don't want them to fly
    i.P = 100000
    i.D = 2000
    i.Position = (root.CFrame*CFrame.new(0,0,-Dash_Normal)).Position --get 20 units in front of the player
    i.Parent = root
    coroutine.wrap(function()
        wait(.2) --however long you want the dash to last
        i:Destroy() -- remove the BodyMover (important!!)
    end)
end

Now that we have a way of moving our character, let’s tie it all together with some sticks and glue.

local UIS = game:GetService('UserInputService')
local Player = game:GetService('Players').LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Root = Character:WaitForChild('HumanoidRootPart')

local Dash_Normal = 20 -- set the default range for better control if you want to change it later on
local Dash_Timeout = 0.15 -- how long will it wait in-between taps; think of this as tap speed
local Can_Dash = false -- can the player dash immediately? no.

function DashForward(root)
    local i = Instance.new('BodyPosition')
    i.MaxForce = Vector3.new(1000000,0,1000000) -- y-component is 0 because we don't want them to fly
    i.P = 100000
    i.D = 2000
    i.Position = (root.CFrame*CFrame.new(0,0,-Dash_Normal)).Position --get 20 units in front of the player
    i.Parent = root
    coroutine.wrap(function()
        wait(.2) --however long you want the dash to last
        i:Destroy() -- remove the BodyMover (important!!)
    end)()
end

UIS.InputBegan:Connect(function(input,istyping)
    if istyping then return end
    if input.KeyCode == Enum.KeyCode.W then
        if Can_Dash == true then
            Can_Dash = false
            DashForward(Root)
        else
            Can_Dash = true
            coroutine.wrap(function()
                wait(Dash_Timeout)
                Can_Dash = false
            end)()
        end
    end
end)

Hope this helped

46 Likes

Oh wow thats incredible. I’ll work on understanding it later, I greatly appreciate your help :)!

2 Likes

I followed that tutorial and got this

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UIP = game:GetService("UserInputService")
local LastTime = tick()


local Debounce = false

UIP.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end 
	if Input.KeyCode == Enum.KeyCode.W then 
		local now = tick() 
		local difference = (now - LastTime)
		if difference <= 1 then
		   if not Debounce then Debounce = true
			wait(1)
			Debounce = false
		else
			Character.HumanoidRootPart.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 200
		end
	end
	end
	end)

no errors, bt still not working. This is in a local script btw.
u see any issues?

Try this one

local t = 0
local dif = tick() - t
UIP.InputBegan:Connect(function(Input, IsTyping) 
	if not IsTyping then
		if Input.KeyCode == Enum.KeyCode.W then  
			dif = tick() - t

			if dif < 1 then  -- Tapped twice under one second
				print("Test")  -- Prints "Test" if tapped twice under a second
				t = 0
				return 
			end 

			t = tick()
		end
	end
end)

I just tried it out and it worked great! TY
Do you know what I’d have to do to have it also work like backwards, left , and right.
With DD, AA, and SS of course.

I know we’d change i.Position = (root.CFrame*Vector3.new(0,0,-Dash_Normal))
and Input, but idk how to make it a seperate one for each.

You can use various elseif statements and adjust the DashForward() a little bit:

function Dash(root,direction)
    local position;
    if direction == 'forward' then
        position = (root.CFrame*CFrame.new(0,0,-Dash_Normal)) --get 20 units in front of the player
    elseif direction == 'backward' then
        position = (root.CFrame*CFrame.new(0,0,Dash_Normal)) --get 20 units behind the player
    -- add more directions here, for left and right dashing, change the CFrame.new() to X instead of Z
    end
    local i = Instance.new('BodyPosition')
    i.MaxForce = Vector3.new(1000000,0,1000000) -- y-component is 0 because we don't want them to fly
    i.P = 100000
    i.D = 2000
    
    i.Parent = root
    coroutine.wrap(function()
        wait(.2) --however long you want the dash to last
        i:Destroy() -- remove the BodyMover (important!!)
    end)
end

Input:

    if input.KeyCode == Enum.KeyCode.W then
        if Can_Dash == true then
            Can_Dash = false
            Dash(Root,'forward')
        else
            Can_Dash = true
            coroutine.wrap(function()
                wait(Dash_Timeout)
                Can_Dash = false
            end)()
        end
    elseif input.KeyCode == Enum.KeyCode.S then
        if Can_Dash == true then
            Can_Dash = false
            Dash(Root,'backward')
        else
            Can_Dash = true
            coroutine.wrap(function()
                wait(Dash_Timeout)
                Can_Dash = false
            end)()
        end
    end
6 Likes

Ty I appreciate your knowledge, I’m gonna study this all later so I can learn to do it on my own lol

1 Like

Of course. Don’t hesitate to DM me if you come across any issues so we don’t bump this thread to high heaven.

6 Likes

How do you make it not glitch you and fling you when you dash into a wall?

2 Likes