Changing only one of the values in a vector3

Hello, I’ve been working on a creating a custom character, I have got this code to change the character position but I’m not sure how to change just one of the vector3 values (In this case, z) without changing the whole entire thing, while keeping the original values of x and y. Also this is in a script. Here is the code so far:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		script.Parent.Char.Position = -- where I'm stuck
end)

How can I do this? :man_shrugging:

3 Likes
local ZPosition = 3 -- Whatever you want
script.Parent.Char.Position = Vector3.new(script.Parent.Char.Position.X,script.Parent.Char.Position.Y,ZPosition)
1 Like

If you want to move the character a little in object space you use

local ZPosition = 0.1
script.Parent.Char.CFrame = script.Parent.Char.CFrame * CFrame.new(0,0,ZPosition)
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		script.Parent.Char.Position = Vector3.new(script.Parent.Char.Position.X, script.Parent.Char.Position.Y, 100)
		-- script.Parent.Char.Position = Vector3.new(getXpos, getYpos, yourZpos) <-- Help Comment
	end
end)

Vector3.new() takes 3 values which are (X, Y, Z). However you can’t change just Z, but you can bypass that by just getting the missing values.

Also if it is a character, position is not a valid member of model so you can use a primary part to move the whole model.
Char:SetPrimaryPartCFrame(CFrame Value)

It does not work, but now I can look around the character.

I’m just using a sphere for it. :slight_smile:

1 Like

Can you show your “Custom Character” in a explorer window.

here is a picture of my explorer: Screen Shot 2020-06-18 at 3.57.47 PM

1 Like

in my case, I’m using a sphere for a physics based game.

Doesn’t Roblox handle the movement for you?

not when using a custom character from my knowledge.

The default control scripts and camera scripts still get inserted. But u don’t have a HumanoidRootPart torso and head.

EDIT: To overwrite the control scripts u use insert a script (local) called ControlScript in starterplayerscripts.

yes but, I don’t want to have humanoidrootpart, torso and head, because the player will be a sphere the whole time, so it would look really odd.


still does not work.
Already read it.

Please read the edit in my previous post

1 Like

Hey I made a sphere example game,
SphereGameExampleByDaw588.rbxl (20.7 KB)
Feel free to look around a code and try to learn from it.

When you hold W, it will increase the speed of the sphere, the sphere will go in direction of where your camera is looking in.

Also game is multiplayer compatible. Have fun coding!

3 Likes

thanks, This was exactly what I was thinking of but in a multiplayer racing game! But how do you remove the character since, the character runs next to the sphere.

I will try to do that, just give me a second.

sure. I hate 30 chars
I’ll make sure to give credit.