Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I change just one axis in a CFrame?

So I wanna make a turret that moves to face a players mouse so I did:

wait(5)

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tank = game.Workspace:FindFirstChild(""..Plr.Name.."'s_Tank")
local mousePosition = script.Parent.GetMousePosition.MousePosition
local FakeTurret = Tank.FakeTurretHitbox

while wait() do
	FakeTurret.CFrame = CFrame.new(FakeTurret.CFrame.p, mousePosition.Value.p)
end

This worked but I only wanted the turret to move to the target on it's y axis, so I did this:

wait(5)

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tank = game.Workspace:FindFirstChild(""..Plr.Name.."'s_Tank")
local mousePosition = script.Parent.GetMousePosition.MousePosition
local FakeTurret = Tank.FakeTurretHitbox

while wait() do
	local cframe = CFrame.new(FakeTurret.CFrame.p, mousePosition.Value.p)
	cframe.x = 0
	cframe.z = 0
	FakeTurret.CFrame = cframe
end

This didn't work though, the output said "18:43:42.868 - x cannot be assigned to 18:43:42.869 - Script 'Players.Player.PlayerGui.TurretMover', Line 13 18:43:42.870 - Stack End" I went to the wiki and found out .x .y and .z are read only. So if I can't change the axis like that how else could I?

1 answer

✓ Accepted answer

I don't believe you can make it do that, but here's another way to do this:

local cframe = CFrame.new(FakeTurret.CFrame.p, Vector3.new(mousePosition.Value.p.x,FakeTurret.CFrame.Y,mousePosition.Value.p.z))

6 comments

0

It just keeps the part at -90, 0, -90..... https://gyazo.com/26fdaf6b4b07e797d77343b3e366175f

0

oops hang on, i thnk you're doing it backwards.

0

retry it now

0

It works, but one more thing if the whole model gets rotated the turret would still be on it's own axis. https://gyazo.com/c568eb4be6d6b325a08fa6a0205b7b3a

0

I can't do it. It's not that important anyway, ask a more advanced scripter :P

0

Ill try doing it myself :p

This discussion is preserved as it was. New answers, comments, and votes are closed.