Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you move a player to the left and right using Velocity?

Hey so I'm making a basketball game, I wan't to make it so that when A player presses a key, it will perform the animation and move the player to either the left or right.

However the way I done it, when a player rotates and presses the key, they can get moved forward and backwards instead of right and left.

Key Press

UIS.InputBegan:Connect(function(key , gpe)
	if key.KeyCode == KeyBindingModule:SwitchHands() and not gpe then
		if animations.right_dribble.IsPlaying then
			animations.right_dribble:Stop()
			animations.switch_left:Play()
			createVelocity(0,0,15)
			animations.switch_left:Stop()
			animations.left_dribble:Play()
		elseif animations.left_dribble.IsPlaying then
			animations.left_dribble:Stop()
			animations.switch_right:Play()
			createVelocity(0,0,-15)
			animations.switch_right:Stop()
			animations.right_dribble:Play()
		end
	end
end)

Velocity Function

local function createVelocity(x , y, z)
	local newVelocity = Velocity:Clone()
	newVelocity.Velocity = Vector3.new(x,y,z)
	newVelocity.Parent = Character.Torso
	wait(.25)
	newVelocity:Destroy()
end

1 comment

0

Take the cross product between the character's HumanoidRootPart's look vector and the y-axis. See here: https://scriptinghelpers.org/questions/61374/#59749

1 answer

✓ Accepted answer

Try creating a velocity relative to the HumanoidRootPart I'm not sure this would work either but at least give it a shot.

ALSO make sure to define the player.

local function createVelocity(x , y, z)
    local newVelocity = Velocity:Clone()
    newVelocity.Velocity = player.Character.HumanoidRootPart.CFrame.LookVector * Vector3.new(x,y,z)
    newVelocity.Parent = Character.Torso
    wait(.25)
    newVelocity:Destroy()
end

If the player moves in the wrong direction, play around with the x,y,z vector offset.

This discussion is preserved as it was. New answers, comments, and votes are closed.