How do i make the player always have the same Z position

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

1 Like

You could fill in the X and Y that they currently have.

Here’s an example:

local position = char.HumanoidRootPart.Position
char.HumanoidRootPart.CFrame = (char.HumanoidRootPart.CFrame - position) + Vector3.new(position.X, position.Y, 50)

This will keep their current rotation too.

2 Likes

i have already tried that. It wont work well, the player moves very laggily. u cant even move that much u get tp:ed back.

So is there any other way to do it?

1 Like

As this post shows you can technically disable the inputs using the ContextActionService.

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
)
3 Likes

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)

the game is a 2d fighting game so that wont work

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)
5 Likes

Thanks so much. i have been trying to solve this the last 2 hours

wait i just now realised 3 days later that that wont change the position on the server only the client. Which creates some problems.

It should, when I tested it, it did. When I tested I put a local script in the startergui and everything worked

oh i saw wrong it is not just the server that it wont work on. It seems like the script dont work? i remade it a little bit maybe i broke it

local lastZpos = Root.CFrame.Z
    RunService.Heartbeat:Connect(function()
    	if lastZpos ~= Root.CFrame.Z then
    		local LastOrientation = Root.Orientation
    		Root.CFrame = CFrame.new(Vector3.new(Root.Position.X, Root.Position.Y, lastZpos)) * CFrame.Angles(math.rad(LastOrientation.X),math.rad(LastOrientation.Y),math.rad(LastOrientation.Z))
    		lastZpos = Root.CFrame.Z
    	end
    end)
1 Like

That’s odd, it works for me, where is your script in?

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.

I tested by printing when it changes and it changes the position but not just right. It sets it to like 49.634 or 51.734 for example

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)
3 Likes