I want the player always to have the same z position im making a 2d game. The player cant move in the z direction but sometimes they get pushed by other players so that their z gets to something else.
i want something like this kinda:
char.HumanoidRootPart.CFrame = CFrame.new(0,0,50)
tho that wont work i know since then the player always get teleported to 0,0 in x and y. I want those still to be the same
local ContextActionService = game:GetService("ContextActionService")
ContextActionService:BindAction(
"DisableZ",
function() return Enum.ContextActionResult.Sink end, --this is "denying" the inputs
false,
Enum.PlayerActions.CharacterLeft, Enum.PlayerActions.CharacterRight --these are the inputs we wanna disable, going left and right, basically along the Z axis, here are all of the player inputs, here are all the inputs https://developer.roblox.com/en-us/api-reference/enum/PlayerActions
)
I said earlier i have disabled those inputs. The problem is that the player will still sometimes get pushed by other players so that they get pushed on the z axis.
Do you want the players to bump into each other, or is walking through each other an option?
If so, this should help you so that the characters do not bump each other and cause problems.
An option would be to have a general part (Usually the baseplate, or whatever the players are standing on since it is stationary so the Z position never moves) and use that part’s Z position to reposition players so that everyone has the same Z position. This also reduces the potential issue that the players’ Z position’s are not consistently in the desired position.
So instead of:
char.HumanoidRootPart.CFrame = CFrame.new(0,0,50)
You have: char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.X, char.HumanoidRootPart.CFrame.Y, baseplate.CFrame.Z)
One way you could make the player always have the same z position is to do something like this, this will always maintain the z position
local player = game.Players.LocalPlayer
local Root = player.Character.HumanoidRootPart
local lastZpos = Root.CFrame.Z
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
if lastZpos ~= Root.CFrame.Z then
Root.CFrame = CFrame.new(Vector3.new(Root.Position.X, Root.Position.Y, lastZpos))
lastZpos = Root.CFrame.Z
end
end)
startergui is it in and there is alot more going on above the “text” but it reaches all the way down to where this thing is. when i say text i mean the script i sent.
Oh fixed it by changing lastpos to 50. Cause when the player gets teleported they might move sometimes making their z change to something else this is fixed by setting lastpos to 50 just all the time.
Now what should i mark as answer? this post or your post?
Whichever one you find more helpful to others i guess, by the way you don’t have to include last orientation something like this works:
RunService.Heartbeat:Connect(function()
if lastZpos ~= Root.CFrame.Z then
Root.CFrame = CFrame.new(Vector3.new(Root.Position.X, Root.Position.Y, lastZpos)) * CFrame.Angles(0, math.rad(Root.Orientation.Y),0) -- limits the z axis, but maintains the orientation of the HumanoidRootPart
lastZpos = Root.CFrame.Z
end
end)