Make player model face direction player is moving

I have this LocalScript that moves the players with wasd and mouse. I can’t seem to figure out a way to have the players custom model rotate to face the direction the player is going
(eg: the player presses s to move backwards, so the model rotates so it faces backwards from where it was).

Here is what I have so far.

And this is the LocalScript

local userInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer.Character:WaitForChild("actualCharacter")
local p1 = game.Players.LocalPlayer.Character:WaitForChild("Head")
local p2 = game.Players.LocalPlayer.Character:WaitForChild("Torso")
local humanoid = script.Parent:WaitForChild("Humanoid")
local mouse = game.Players.LocalPlayer:GetMouse()

humanoid.WalkSpeed = 0
humanoid.JumpPower = 0

local distance = 8
local moveTime = .2
local squishTime = .7
local moveTweenInfo = TweenInfo.new(moveTime, Enum.EasingStyle.Quart)
local squishTweenInfo = TweenInfo.new(squishTime, Enum.EasingStyle.Quart)
local tweenEnabled = false

--squish start variables
local tweenStart = TweenService:Create(player, squishTweenInfo, {Size = Vector3.new(player.Size.X, 10.55 - 2.5, player.Size.Z)}, {Position = CFrame.new(player.Position.X, player.Position.Y - 2, player.Position.Z)})
local tweenStart2 = TweenService:Create(p1, squishTweenInfo, {Size = Vector3.new(p1.Size.X, 10.55 - 2.5, p1.Size.Z)})
local tweenStart3 = TweenService:Create(p2, squishTweenInfo, {Size = Vector3.new(p2.Size.X, 10.55 - 2.5, p2.Size.Z)})

--squish end variables
local tweenEnd = TweenService:Create(player, squishTweenInfo, {Size = Vector3.new(player.Size.X, 10.55, player.Size.Z)})
local tweenEnd2 = TweenService:Create(p1, squishTweenInfo, {Size = Vector3.new(p1.Size.X, 10.55, p1.Size.Z)})
local tweenEnd3 = TweenService:Create(p2, squishTweenInfo, {Size = Vector3.new(p2.Size.X, 10.55, p2.Size.Z)})

--commences the squishing
userInputService.InputBegan:Connect(function(input, Processed)
	if not Processed then
		if tweenEnabled == true then return end	
		if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
			tweenStart:Play()
			tweenStart2:Play()
			tweenStart3:Play()
		elseif input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down then
			tweenStart:Play()
			tweenStart2:Play()
			tweenStart3:Play()
		elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
			tweenStart:Play()
			tweenStart2:Play()
			tweenStart3:Play()
		elseif input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
			tweenStart:Play()
			tweenStart2:Play()
			tweenStart3:Play()
		end	
	end
end)

--releases the squishing and does movement
userInputService.InputEnded:Connect(function(input, Processed)
	if not Processed then
		if tweenEnabled == true then return end	
		if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
			tweenEnd:Play()
			tweenEnd2:Play()
			tweenEnd3:Play()
			frontMove()	
		elseif input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down then
			tweenEnd:Play()
			tweenEnd2:Play()
			tweenEnd3:Play()
			backMove()
		elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
			tweenEnd:Play()
			tweenEnd2:Play()
			tweenEnd3:Play()
			leftMove()
		elseif input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
			tweenEnd:Play()
			tweenEnd2:Play()
			tweenEnd3:Play()
			rightMove()
		end		
		task.delay(squishTime, function() tweenEnabled = false end)
	end
end) 

--commences the squishing for mouse
mouse.Button1Down:Connect(function()
	if tweenEnabled == true then return end	
	tweenStart:Play()
	tweenStart2:Play()
	tweenStart3:Play()
end)

--releases the squishing and does movement for mouse
mouse.Button1Up:Connect(function()
	if tweenEnabled == true then return end	
	tweenEnd:Play()
	tweenEnd2:Play()
	tweenEnd3:Play()
	task.delay(squishTime, function() tweenEnabled = false end)
	frontMove()	
end) 

--movement happens here
function frontMove()
	tweenEnabled = true	
	local tween = TweenService:Create(player, moveTweenInfo, {CFrame = player.CFrame * CFrame.new(0, 0, -distance)})
	tween:Play()
	tween.Completed:Wait()
	tweenEnabled = false
end

function backMove()
	tweenEnabled = true	
	local tween = TweenService:Create(player, moveTweenInfo, {CFrame = player.CFrame * CFrame.new(0, 0, distance)})
	tween:Play()
	tween.Completed:Wait()
	tweenEnabled = false
end

function leftMove()
	tweenEnabled = true
	local tween = TweenService:Create(player, moveTweenInfo, {CFrame = player.CFrame * CFrame.new(-distance, 0, 0)})
	tween:Play()
	tween.Completed:Wait()
	tweenEnabled = false
end

function rightMove()
	tweenEnabled = true
	local tween = TweenService:Create(player, moveTweenInfo, {CFrame = player.CFrame * CFrame.new(distance, 0, 0)})
	tween:Play()
	tween.Completed:Wait()
	tweenEnabled = false
end
1 Like

get the unit vector of your move vector (a vector that has nunbers between 0-1) then use that with cframe.lookat() and model:setprimarypartcframe()

1 Like