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

lookVector cannot be assigned to?

lookVector cannot be assigned to

game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
player = game.Players.LocalPlayer
hum = player.Character
hum.Humanoid.JumpPower = 0
if hum.Torso ~= nil then
function onKeyPress(inputObject, gameProcessedEvent)
	game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
player = game.Players.LocalPlayer
hum = player.Character
hum.Humanoid.JumpPower = 0
	if inputObject.KeyCode == Enum.KeyCode.W then
		hum.Torso.CFrame.lookVector = Vector3.new(-0, -0, -1)
		hum.Torso.CFrame = hum.Torso.CFrame + Vector3.new(0,0,-5)
	elseif inputObject.KeyCode == Enum.KeyCode.S then
		hum.Torso.CFrame = hum.Torso.CFrame + Vector3.new(0,0,5)
	elseif inputObject.KeyCode == Enum.KeyCode.A then
		hum.Torso.CFrame = hum.Torso.CFrame + Vector3.new(-5,0,0)
	elseif inputObject.KeyCode == Enum.KeyCode.D then
		hum.Torso.CFrame.lookVector = Vector3.new()
		hum.Torso.CFrame = hum.Torso.CFrame + Vector3.new(5,0,0)
		end
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

i have this grid script that move the player in a grid of 5 studs. i want it to change the lookvector too, but apparantly,

lookVector cannot be assigned to

i get the above error whenever i run this, how do i get it to work right?

2 comments

1
  1. lookVectors are a read-only value, which means that you cannot set them yourself. 2) A lookVector must be a unit vector (i.e. it must be one unit long). What are you even trying to do at line 19 by setting the lookVector to 0,0,0? That doesn't make sense.
0

Use CFrame.Angles instead.

1 answer

.lookVector is a read-only property of CFrames, which means that you cannot edit it yourself directly. However, there are indirect ways of "setting" the lookVector of a CFrame. For example:

yourCFrame = CFrame.new( yourCFrame.p, yourCFrame.p + yourLookVector)

will change yourCFrame so that it has the given yourLookVector. Line 12 in your script would be:

hum.Torso.CFrame = CFrame.new( hum.Torso.Position, hum.Torso.Position + Vector3.new(-0, -0, -1) )

Mind you that some information is lost by setting the CFrame's lookVector this way - namely, the rotation on its z-axis. but it doesn't seem like it would matter in your case. You may want to look at CFrame.fromAxisAngles instead if you want to preserve the CFrame's rotation on its z-axis.

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