So I’m making a fighting game like Street Fighter.
Which means there will be two players going against each other and I need them to be facing in a certain direction (which should be towards their opponent)
So far I “coded” (took a script off the toolbox and modified it a bit) that forces the players (x) position to be a certain spot. So far it has worked perfectly fine with the side affect of making the player look in a certain direction. Which is exactly what I want, but the problem is that it makes the second player face the wrong direction, away from their opponent. (they only face the opposite way after they first move due to how the script updates)
Example of the code:
local player = game.Players.LocalPlayer
local Root = player.Character.HumanoidRootPart
local lastXpos = Root.CFrame.X
local RunService = game:GetService("RunService")
CorrectTeam = game.Teams:findFirstChild("Gold") --I have a script for the other team which currently does the same thing
TeamColour = CorrectTeam.TeamColor
if player.TeamColor == TeamColour then
RunService.Heartbeat:Connect(function()
if lastXpos ~= Root.CFrame.X then
Root.CFrame = CFrame.new(Vector3.new(lastXpos, Root.Position.Y, Root.Position.Z))
lastXpos = Root.CFrame.X
end
end)
end
Preferably I would like the players to be facing toward the opponent (only in 0 degrees and 180 degrees).
But first I would like to known why this code doesn’t work, and how I should change it so player 2 is facing the correct way.
Player 1’s view:
Player 2’s view before moving (ignore how the camera is in the wrong position):
You don’t have enough parameters to CFrame.new. From the documentation, the first Vector3 is for position. The second Vector3 is for orientation (aka look vector). But, if you’re looking to specify a specific angle, the documentation has the code that you are looking for:
-- A canonical method of creating a CFrame at a certain position and Euler rotation (XYZ).
local cf = CFrame.new(0, 5, 0) * CFrame.Angles(math.rad(45), 0, 0)
I wonder if the player not facing the correct way (until you move) may be something related to replication, I am only assuming this is the case since I have never really dealt with this situation before, but I think that the other players position / rotation etc are not being replicated on-spawn until your position changes, I can see that Player1 is on a SpawnLocation,
I would maybe look into spawning each player elsewhere and teleporting them to the correct positions when the round starts, perhaps maybe that way roblox will replicate it and you will be seeing the correct output? Alternatively, you could force the player to jump, which should in theory tell Roblox to replicate since your character has moved. You could mask this by blanking their screen while they are teleporting/jumping.
Unless I am completely miss-understanding the issue here, and the character is only being rotated because you moved in that direction handled by Roblox, apologies if so.
Usually once you set values in a Vector3, you need to create a new Vector3 when you need to change them. Yeah, I know its a pain to deal with, but that’s the reality that we work with.