Trying to script Directional Movement

Hi, I’m trying to make a directional dash action. I’ve looked everywhere and most scripts about dashing aren’t directional and only go one way.

I’ve been trying to find a way to detect if a player is going a certain direction and if so, the dash goes the direction they are going to. (Example: Player is moving to the left, he automatically dashes to the left as that is where he is moving.)

I’ve tried Humanoid Direction and CFrame but none seem to work.

Here is an example of what I want to get:

local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Debounce = false

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftControl then
		if not Debounce then Debounce = true
			wait(1)
			Debounce = false
		else
			if -- Code about where the player's direction is going plays the rest of the dash action
			local BodyPosition = Instance.new("BodyPosition")
			BodyPosition.MaxForce = Vector3.new(1000000,0,1000000)
			BodyPosition.P = 100000
			BodyPosition.D = 2000
			BodyPosition.Position = (HumanoidRootPart.CFrame*CFrame.new(0,0,10)).Position
			BodyPosition.Parent = HumanoidRootPart
			wait()
			BodyPosition:Destroy()
		end
	end
end)

This isn’t the actual script I made, but something I envision the script would look like.

Would checking if the player pressed WASD work for this?

Like if W with the user input service then whatever. If that makes sense.

Also, I would use animations for this. But that might not be the best way to do this.

Well, you’re just in luck! I’ve made something of the sort, though it might be a little dated :confused:, I won’t be showing anything, but I’ll explain how it works!

First, we instantiate a table that’ll hold the keys and another of accepted keys, then upon pressing, add to that table and that’ll be it’s direction. It’d look something like this:

local Keys = {
	W = Enum.KeyCode.W;
	A = Enum.KeyCode.A;
	S = Enum.KeyCode.S;
	D = Enum.KeyCode.D;
};
local Is_Pressing = {};

local Current_Key = Keys["Enum.KeyCode.S"];

UIS.InputBegan:Connect(function(key, gameProcessedEvent)
	for Index,Key in pairs(Keys) do
		if (UIS:IsKeyDown(Key)) and not(Current_Key == Key) and not(table.find(Is_Pressing, Key)) then
			table.insert(Is_Pressing, Key);
			Current_Key = Key;
			break
		end

And for a removing function:

UIS.InputEnded:Connect(function(key, gameProcessedEvent)
	local Key = string.gsub(tostring(key.KeyCode), "Enum.KeyCode.", "");
	if Keys[Key] then
		for index,value in pairs(Is_Pressing) do
			--print(index, value, UIS:IsKeyDown(value));
			if not(UIS:IsKeyDown(value)) then
				table.remove(Is_Pressing, index);
			else
				Current_Key = value;
			end
			if (#Is_Pressing <= 0) then
				Current_Key = Keys["Enum.KeyCode.S"];
			end
		end
	end

With these functions, you can kind of structure how you want your dashes to work, this is how I went about it because I sent the Current_Key as a remote event argument.

If you’re striving to make it all local, then you can do a watered down version of this that doesn’t need to necessarily have a current_key, and just check if the buttons are being held, maybe make the table that has the accepted keys as bools for when they’re being held or not, and changed based on that.
I hope I helped, if you have any questions, just ask me! :grinning_face_with_smiling_eyes:

1 Like

As for the ‘iframes’ part, if you’re trying to replicate that, create a custom hitbox function, and make a whitelist for when someone can’t be hit and enable it once you’re rolling, fairly simple stuff.

I’m not interested in the iframes part (yet), though I might have to make a local script version as I don’t know how to define a character on a server script without it being nil. Is there a way to get the player’s character on server side so that I can access HumanoidRootPart. Or will I have to use a local script?

To define a character on the server, first you must take advantage of client > server communication. Look into RemoteEvent’s and RemoteFunction’s!

1 Like