Good way of making a dash action?

Hey. I’ve been looking to make a dash to a game that I am working on, but I can’t really think of a way of making the dash action itself. Here is the script so far that I am using, with a video:
https://streamable.com/wxwfkf

local userInputService = game:GetService("UserInputService");
local tweenService = game:GetService("TweenService");
local players = game:GetService("Players");
local localPlayer = players.LocalPlayer;
local cooldown = script:WaitForChild("Cooldown");
local keys = {
	Enum.KeyCode.A;
	Enum.KeyCode.S;
	Enum.KeyCode.D;
	Enum.KeyCode.W;
};
local currentKey = nil;

function input_Began(input, gpe)	
	if gpe then return end;
	if cooldown.Value then return end;
	if not table.find(keys, input.KeyCode) then return end;
		
	if input.KeyCode == Enum.KeyCode.A then
		currentKey = Enum.KeyCode.A
		local pressTime = tick();
		local isPressedAgain = false;
		repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.A then
					isPressedAgain = true
				end
			end
			
			userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5
		if isPressedAgain and currentKey == Enum.KeyCode.A then
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(0, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(.5), {TextTransparency = 0}):Play()
			cooldown.Value = true
			wait(.7)
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(1, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(1.3), {TextTransparency = 1}):Play()
			wait(1.3)
			cooldown.Value = false
		end
	end
	
	if input.KeyCode == Enum.KeyCode.S then
		currentKey = Enum.KeyCode.S
		local pressTime = tick();
		local isPressedAgain = false;
		repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.S then
					isPressedAgain = true
				end
			end

			userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5
		if isPressedAgain and currentKey == Enum.KeyCode.S then
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(0, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(.5), {TextTransparency = 0}):Play()
			cooldown.Value = true
			wait(.7)
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(1, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(1.3), {TextTransparency = 1}):Play()
			wait(1.3)
			cooldown.Value = false
		end
	end
	
	if input.KeyCode == Enum.KeyCode.D then
		currentKey = Enum.KeyCode.D
		local pressTime = tick();
		local isPressedAgain = false;
		repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.D then
					isPressedAgain = true
				end
			end

			userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5
		if isPressedAgain and currentKey == Enum.KeyCode.D then
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(0, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(.5), {TextTransparency = 0}):Play()
			cooldown.Value = true
			wait(.7)
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(1, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(1.3), {TextTransparency = 1}):Play()
			wait(1.3)
			cooldown.Value = false
		end
	end
	
	if input.KeyCode == Enum.KeyCode.W then
		currentKey = Enum.KeyCode.W
		local pressTime = tick();
		local isPressedAgain = false;
		repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.W then
					isPressedAgain = true
				end
			end

			userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5
		if isPressedAgain and currentKey == Enum.KeyCode.W then
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(0, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(.5), {TextTransparency = 0}):Play()
			cooldown.Value = true
			wait(.7)
			tweenService:Create(script.Parent:WaitForChild("Dash").Line, TweenInfo.new(.5), {Size = UDim2.new(1, 0, 1, 0)}):Play()
			tweenService:Create(script.Parent:WaitForChild("Dash").Cooldown, TweenInfo.new(1.3), {TextTransparency = 1}):Play()
			wait(1.3)
			cooldown.Value = false
		end
	end
end;

userInputService.InputBegan:Connect(input_Began)

Note: I am not looking for a script, I am looking for any possible way of making a dash action.

Would changing player’s WalkSpeed be acceptable? I think it is a easy way how to do this and avoid other issues. You can change it using the TweenService to get a smoother effect, but it’s your choice!

EDIT: It would also be great to use :Disconnect(), as I see you are using in your script a bunch of :Connect()s. It is not really best for the preformance.

EDIT 2: For fix of the preformance issue with using bunch of :Connect()s I’d change this part of script:

repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.S then
					isPressedAgain = true
				end
			end

			userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5

to this:

		local function input_Began2(i, g)
			if g or not table.find(keys, i.KeyCode) then return end;
			if input.KeyCode == Enum.KeyCode.S then
				isPressedAgain = true
			end
		end

		local con = userInputService.InputBegan:Connect(input_Began2)
		repeat wait() until isPressedAgain == true or tick() - pressTime >= 0.5
        con:Disconnect()

I already thought of disconnecting the events, but I can’t really reference the connections later on.
Edit:

Oh, and have you tried the modification that I made for you in my last post?

Maybe this could be useful to you? You can make actions and bind/unbind them whenever you need to.

Oh wait, this worked:

        repeat wait()
			local function input_Began2(i, g)
				if g or not table.find(keys, i.KeyCode) then return end;
				if input.KeyCode == Enum.KeyCode.W then
					isPressedAgain = true
				end
			end

			connection4 = userInputService.InputBegan:Connect(input_Began2)
		until isPressedAgain == true or tick() - pressTime >= 0.5
		connection4:Disconnect()

I don’t need buttons for mobile, I need an idea on how to make a dashing action.

This is not a great way to do that. When you make it like this, you are connecting it every singe time the wait() is finished. Try my version!

EDIT: It’s in this post.

Fixed it:

        local function input_Began2(i, g)
			if g or not table.find(keys, i.KeyCode) then return end;
			if input.KeyCode == Enum.KeyCode.A then
				isPressedAgain = true
			end
		end

		local connection = userInputService.InputBegan:Connect(input_Began2)
		
		repeat wait()
			
		until isPressedAgain == true or tick() - pressTime >= 0.5
		
	    connection:Disconnect()
1 Like

Yep, that looks great! It looks just like my version, you have just changed the variable name, but great job anyways!

Thank you! I’ll sign your answer as a solution and implement it in my code. Much thanks! :slight_smile: :pray:

1 Like