Multi directional dodges in 2d space (Top View)

This is the basic layout, I’m not allowed to write full scripts on the DevForum as you need to learn too :smiley:.

--dictionary and local userInput here.

userInput.InputBegan:Connect(function(input)
     if input.KeyCode == Enum.KeyCode.Space then
        --put the for loop in here.

        --Your tween.
     end
end)
1 Like

You helped me out in a much greater way then I could ever wish for.
Much obliged !
anyho I better change the title of this threat, because it doesn’t fit the answer.

1 Like

I’m glad I could help! I’d love to see the result (when it’s done).

“Unable to cast Instance to token” at line

local keyCFrame = keyDir[userInput:GetStringForKeyCode(v)]
if keyCFrame then cframeToGoTo = cframeToGoTo * keyCFrame end

i have no bloody idea what that means …

I don’t think it has to do anything with that line as we are never specifying a instance. I think it’s your tween if you have made that yet. Here is a good way to make up the tween:

tweenServ:Create(char.HumanoidRootPart, TweenInfo.new(3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false), {CFrame = cframeToGoTo}):Play()

1 Like

pretty sure i did everything correctly but error persists ant the exact same line

local TweenService = game:GetService("TweenService")
local cframeToGoTo = char.Torso.CFrame

TweenService:Create(char.HumanoidRootPart, TweenInfo.new(3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false), {CFrame = cframeToGoTo})
for _,v in pairs(userInput:GetKeysPressed()) do

local keyCFrame = keyDir[userInput:GetStringForKeyCode(v)]

if keyCFrame then cframeToGoTo = cframeToGoTo * keyCFrame 
end end end end)

From my understanig we are creating another root part that is welded to player and applay cframe force.
right ?

If this is your complete script your order is completely wrong, the tween should be at the bottom and where is the InputBegan event? Also you must specify char! local char = game.Players.LocalPlayer.Character.

We are not creating nor cloning a rootpart, I’ll explain the script:

Once space gets pressed (InputBegan) we create a variable called cframeToGoTo (local cframeToGoTo = char.HumanoidRootPart.CFrame), next we check loop through every key that is currently pressed and we check if it has a linked cframe (the dictionary). Next we multiply that with the cframeToGoTo variable, let’s say that the A and S keys are currently pressed, and the position of the HumanoidRootPart is exactly 0, 0, 0, we will add 10 studs on the X axis and 10 studs on the Z axis because the keys correspond to that cframe value on the dictionary, at the end we’d have: CFrame.new(10,0,10), next we create a tween which will smoothly move the HumanoidRootPart to our result cframe.

If the script above is your full script you are missing alot of things!

1 Like

Well its not, i know about wariables and how to do them properly, this time i just posted “important parts of code” at least in my opinion.
and here is the final version


local char = game.Players.LocalPlayer.Character 
local userInput = game:GetService("UserInputService") 
local TweenService = game:GetService("TweenService")
local cframeToGoTo = char.Torso.CFrame

local humanoid = char:WaitForChild("Humanoid")	

if not char then wait()end

userInput.InputBegan:Connect(function(input)
  if input.KeyCode == Enum.KeyCode.Space then
	
local keyDir = 
 {["S"] = CFrame.new(0,0,10),
  ["D"] = CFrame.new(-100,1,0),
  ["W"] = CFrame.new(0,0,-10),
  ["A"] = CFrame.new(10,0,0),}

for _,v in pairs(userInput:GetKeysPressed()) do
local keyCFrame = keyDir[userInput:GetStringForKeyCode(v)] -- this line
if keyCFrame then cframeToGoTo = cframeToGoTo * keyCFrame 
end end end	end)

TweenService:Create(char.HumanoidRootPart, TweenInfo.new(3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false), {CFrame = cframeToGoTo})
	

there is one extra variable for later
but the problem still persists

“Unable to cast Instance to token” on line 20

Works like a dream.
Was that code from earlie defected in the core or you just got pissed off and revrited everything completly ?

Oh now i see bits and pieces of that code from earlier.

I reused parts and I figured out what was wrong, after patching them it would do some weird movements if I used tweenService. That’s why I switched it to use the :MoveTo() function of the humanoid.

And I might upload it to roblox if I see a familiar topic.

The contextActionService’s use is to simply block player input.

1 Like

I remember trying to do it in a similar way (much simpler way) but scraped the concept after figuring out that when the player is holding the button and then performs an action he would have to re-press the buttons to continue moving or in other words, the game would forget what inputs were sent before the action.

That’s the biggest minus of unbinding.

oh and i found out that it will soft lock player if there is something in the way
remowing humanoid.MoveToFinished:Wait() fixes it.
maybe i should add if is something in the way then wait()

Damn it feels quite janky to use and work with,
unbind is messing up my stuff.

maybe my old build wasn’t that bad.

I don’t understand you, first you say that it works like a dream and now it’s janky? That is quite rude to say if you keep in mind that I did this willingly…

I said it after longer use and a few experiments.
I really appreciate your help but as it turns out my thingy from the start runs smoother it was much more tiresome to write but as long as it works.

Just as a comparison

Yours with a few extra features and a bit of optimization


And my old somewhat finished script

My biggest issue with is that your script blocks all inputs for a moment and thos forgets that player is still holding movement buttons forcing player to re-press them constantly after every dodge making for an unpleasant experience.

As of now, I’m working on invincibility frames and on later date animation.

I’m sorry if you feel used by me but in the end, you suggested that alternative.

Just a suggestion: Why don’t you use MoveDirection?

1 Like

That’s a great suggestion, works perfectly fine.
I trashed my old script and replaced it with a modified scrip from that video using MoveDirection.

here is a showcase (a cube is there just for visualization)


It should even work with a controller and mobile devices.

1 Like