Make the player automatically move in a desired direction

Im trying to make a roll script where when you press space, your character rolls in that direction, i know how to make the animation play and increase the speed, all that stuff, but when it comes to making the player move in the direction they were moving when they pressed space, i just don’t know how to do it.

I’ve tried a few things but all of them end up not working or having a bug

Something i have tried was when the player presses space while moving in a direction, it would make any action they make be ignored using Context Action Service and move them in that direction using Player:Move(), the player would stand still after doing a roll even if he was holding a button to move because of Context Action Service

Example: Player holds forward (W) and starts moving forward, the Player presses Space and they do a roll forward, after the Player finishes rolling they stop moving even tho the Player has not let go of the forward(W) key, so the Player would need to let go of the forward (W) key and press it again to move forward.

Sorry if this is long and confusing

3 Likes

Change the movement script, and that should work for you

1 Like

…bruh -_- bruh… …

2 Likes

Bruh what? If you’re looking to move, then just tween in the direction you are facing

1 Like

I’m looking for a different way to do it
Using the player to move
Also my character always looks at the camera so i cant to it in a direction my characters facing

1 Like
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character

local Controller = require(Player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()

local getMoveVector = (function ()
	return Controller:GetMoveVector()
end)
local getRelativeMoveVector = (function ()
	local vec = getMoveVector()
	return game.Workspace.CurrentCamera.CFrame:inverse():VectorToObjectSpace(vec)
end)

game:GetService('RunService').RenderStepped:connect(function ()
	print(getMoveVector(), "relative to cam -->", getRelativeMoveVector())
end)

getMoveVector() will give you a Vec3 of the moving direction of the character e.g. if they’re moving forwards it’ll be Vec3(0, 0, -1)

getRelativeMoveVector() will give you a Vec3 of the moving direction relative to the camera - same as above, but dependent on where the camera is looking

Now that you know the MoveVector of the character, you can multiply that by the speed / dist of your movement and apply it to the Player’s current position to propel them forwards.

Ah. You won’t need the getRelativeMoveVector() function - just use the getMoveVector() function and do as I mentioned above.

2 Likes

I think its easier if just gave a video example

btw Isocortex tnx for this i will definitely use this in the future

1 Like

1 Like

In this clip i am moving forward then i press space to do a roll
I don’t release the forward button but my character stops moving after the roll
I am using a Player:Move() method and i would just like to have the character continue moving forward after the roll

1 Like
script:WaitForChild("Activate").OnClientEvent:Connect(function(Time)
	
	print(Time)
	Movement:Disable() -- disables movement
	game.Workspace.Scripts.SettingValues.Spid:FireServer(5)-- decreases speed
	
	for i = 1, Time do
		
		i = i + 1
		Player:Move(Vector3.new(0,0,-1), true)
		wait()
		
	end
	
	Movement:Enable() -- enables movement
	game.Workspace.Scripts.SettingValues.Spid:FireServer(10) -- increases speed
	
end)

this is the script that moves the player
1 Like

Is there any reason you need to disable the movement? I would probably just instantiate a BodyVelocity on the Server when the spacebar is pressed, vectored to the MoveVector, and then delete it afterwards e.g.

--[!] CLIENT
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character

-- environment
local Controller   = require(Player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
local InputService = game:GetService('UserInputService')
local RollRemote   = game:GetService('ReplicatedStorage'):WaitForChild 'RollRemote'

-- private
local getMoveVector = (function ()
	return Controller:GetMoveVector()
end)

local getRelativeMoveVector = (function ()
	local vec = getMoveVector()
	return game.Workspace.CurrentCamera.CFrame:inverse():VectorToObjectSpace(vec)
end)

-- init
InputService.InputBegan:connect(function (input, gameProcessed)
	if gameProcessed then
		return
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = tostring(input.KeyCode):sub(14)
		
		if key == "Space" then
			RollRemote:FireServer(getRelativeMoveVector())
		end
	end
end)

and then:

--[!] SERVER
-- globals
local ROLL_SPEED = 100 -- we can change these two for the speed/amount of time we roll
local ROLL_TIME  = 1

-- private
local create = function (obj)
	obj = Instance.new(obj)
	return function (props)
		for prop, val in next, props do
			obj[prop] = val
		end
		return obj
	end
end

local isAlive = function (Player)
	local char = Player.Character
	if not char or not char:IsDescendantOf(game.Workspace) or not char:FindFirstChild "Humanoid" or char.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	return true
end

-- set up
local remote = create "RemoteEvent" {
	Name   = "RollRemote";
	Parent = game:GetService('ReplicatedStorage');
}

-- public
local rolling = { }
remote.OnServerEvent:connect(function (player, vec)
	if not vec or not typeof(vec) == "Vector3" or vec.magnitude > Vector3.new(1, 1, 1).magnitude then
		return -- the magnitude check here is to make sure that players aren't sending you vectors that are greater than the unit vector of their movement direction
	end
	if isAlive(player) and not rolling[player] then -- we're checking to see if they're in the 'rolling' table to ensure they're not spamming it, which would create multiple bodyvelocities
		local root, hum = player.Character:FindFirstChild 'HumanoidRootPart', player.Character:FindFirstChild 'Humanoid'
		if root and hum then
			local prevWalk = hum.WalkSpeed
			
			rolling[player] = true
			hum.WalkSpeed = 0 -- we can remove their walkspeed here to stop them from being able to move whilst they roll other than through their bodyvelo
			
			local velo = create "BodyVelocity" {
				Parent      = root;
				MaxForce = Vector3.new(1, 0, 1) * 10000;
			}
			velo.Velocity = vec * ROLL_SPEED
			delay(ROLL_TIME, function () -- after we've rolled for our preset time, we can reset everything
				if velo then
					velo:Destroy()
					hum.WalkSpeed = prevWalk
				end
				rolling[player] = false
			end)
		end
	end
end)
9 Likes

There are many ways to do this. You could simply change the Character’s Velocity Movement, you could use lookVector(), or, you could even do what @Isocortex has stated.

1 Like

This is what i needed, Thanks so much!!!
sorry for the late response

3 Likes