How can I improve this 2D player facing code?

Heyo I’m trying to rewrite my code for making 2 players always face each other however I keep running into issues with either the other player does a slow turn around or movement is shaky and I just don’t know what I can do to get this to work.

Heres a clip of what it currently looks like and as you can see it’s a bit buggy.


I want players to instantly turn towards each other and also to obviously have smooth walking. Here’s my coding for this at the moment.

Main.Remotes.RemoteEvent.OnClientEvent:Connect(function(StartingFacingDirection,Opponent)
	Modules.Camera:Setup()

	if StartingFacingDirection == -1 then
		Modules.Character:PlayAnimation("Idle")
	else
		Modules.Character:PlayAnimation("IdleFlipped")
	end

	Main.Character.FacingDirection:GetPropertyChangedSignal("Value"):Connect(function()
		warn("Changed")
		Modules.Character:UpdateDirection(Main.Character.FacingDirection.Value)	
		if Main.Character.FacingDirection.Value == -1 then
			Modules.Character:StopAnimation()
			Modules.Character:PlayAnimation("IdleFlipped")
		else
			Modules.Character:StopAnimation()
			Modules.Character:PlayAnimation("Idle")
		end
	end)
	
	Main.Character.FacingDirection.Value = StartingFacingDirection
	
	Main.RunService.RenderStepped:Connect(function()
		--> Prevent player from moving on Z axis (I think?)
		local MiddlePoint = workspace.Model.MiddlePoint
		Main.HRP.CFrame = CFrame.new(Main.HRP.CFrame.X,Main.HRP.CFrame.Y,MiddlePoint.CFrame.Z)

		local FacingDirection = math.floor(Opponent.Character.HumanoidRootPart.CFrame.X-Main.HRP.CFrame.X)
		if FacingDirection < 0 then
			Main.Character.FacingDirection.Value = -1
		else
			Main.Character.FacingDirection.Value = 1
		end

		Modules.Camera:UpdateCam()
	end)	
end)

function Character:UpdateDirection(FacingDirection)
	Character.FacingDirection = FacingDirection
	local NewPos = Main.HRP.Position+Vector3.new(FacingDirection,0,0)
	Main.HRP.CFrame = CFrame.new(Main.HRP.Position,NewPos)		
end

1 Like
local IdleTable = {
    [-1] = "Idle",
    [1] = "IdleFlipped"
}

Main.Remotes.RemoteEvent.OnClientEvent:Connect(function(StartingFacingDirection, Opponent)
	Modules.Camera:Setup()
	Modules.Character:PlayAnimation(IdleTable[StartingFacingDirection])

	Main.Character.FacingDirection:GetPropertyChangedSignal("Value"):Connect(function()
		warn("Changed")

		Modules.Character:UpdateDirection(Main.Character.FacingDirection.Value)	
		Modules.Character:StopAnimation()
		Modules.Character:PlayAnimation(IdleTable[-Main.Character.FacingDirection.Value])
	end)
	
	Main.Character.FacingDirection.Value = StartingFacingDirection
	
	Main.RunService.RenderStepped:Connect(function()
		--> Prevent player from moving on Z axis (I think?)
		local MiddlePoint = workspace.Model.MiddlePoint
		Main.HRP.CFrame = CFrame.new(Main.HRP.CFrame.X, Main.HRP.CFrame.Y, MiddlePoint.CFrame.Z)

		local FacingDirection = math.floor(Opponent.Character.HumanoidRootPart.CFrame.X - Main.HRP.CFrame.X)

		--> add 0.5 in case FacingDirection is equal to 0
		Main.Character.FacingDirection.Value = math.sign(math.sign(FacingDirection) + 0.5)
		Modules.Camera:UpdateCam()
	end)	
end)
function Character:UpdateDirection(FacingDirection)
	Character.FacingDirection = FacingDirection
	local NewPos = Main.HRP.Position + Vector3.new(FacingDirection, 0, 0)
	Main.HRP.CFrame = CFrame.new(Main.HRP.Position, NewPos)		
end

tried making the code a bit better

also I am not going to change this because I am on a train, but you can disable a key using ContextActionService instead of running code every RenderStepped, this would be make it so going on the z axis isn’t possible

thanks for making things neater. I have made my own code and adjusted some code in the roblox player modules and so A,D move left and right, S crouch and W jump so you can’t move on the z axis however when you go to move left or right it does turn them


although this is without using

local MiddlePoint = workspace.Model.MiddlePoint
Main.HRP.CFrame = CFrame.new(Main.HRP.CFrame.X, Main.HRP.CFrame.Y, MiddlePoint.CFrame.Z)

When using thiat code this happens

honestly I feel like gameplay would be much more enjoyable for the first video

the second video when you are on one side you can’t turn around at all

though the first video has some bugs, it can be fixed

lol that is kind of the point though I’m making a street fighter game and so I want the players to always be facing each other

my bad I saw the video wrong

I thought they were turning because it was halfway on the map not because a player went past the other player

:joy:

oh LOL it’s alright anyways I swear I’ve been trying to do this for a while now I’m starting to feel like this is something something you just can’t do in Roblox there always seems to be some replication issue or something. I haven’t tried using bodypositions or gyros but I’m not sure how I’d use them.

My guess is the other player is jittering because Humanoid.AutoRotate is enabled in your original video, which is rotating the player’s torso to face towards the movement direction.