What does * has to do at the left of a line

I was browsing through the Roblox documentation for UserInputService and I found this code:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)

local mouse = player:GetMouse()

local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
	FieldOfView = nil,
	_CFrame = nil,
	MouseBehavior = nil,
	MouseDeltaSensitivity = nil,
}

local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0

-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
	target = nil
	camera.CameraType = Enum.CameraType.Custom
	camera.CFrame = originalProperties._CFrame
	camera.FieldOfView = originalProperties.FieldOfView

	UserInputService.MouseBehavior = originalProperties.MouseBehavior
	UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end

local function ZoomCamera()
	-- Allow camera to be changed by script
	camera.CameraType = Enum.CameraType.Scriptable

	-- Store camera properties before zoom
	originalProperties._CFrame = camera.CFrame
	originalProperties.FieldOfView = camera.FieldOfView
	originalProperties.MouseBehavior = UserInputService.MouseBehavior
	originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity

	-- Zoom camera
	target = mouse.Hit.Position
	local eyesight = head.Position
	camera.CFrame = CFrame.new(eyesight, target)
	camera.Focus = CFrame.new(target)
	camera.FieldOfView = 10

	-- Lock and slow down mouse
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseDeltaSensitivity = 1

	-- Reset zoom angles
	AngleX, TargetAngleX = 0, 0
	AngleY, TargetAngleY = 0, 0
end

-- Toggle camera zoom/unzoom
local function MouseClick()
	if zoomed then
		-- Unzoom camera
		ResetCamera()
	else
		-- Zoom in camera
		ZoomCamera()
	end

	zoomed = not zoomed
end

local function MouseMoved(input)
	if zoomed then
		local sensitivity = 0.6 -- anything higher would make looking up and down harder; recommend anything between 0~1
		local smoothness = 0.05 -- recommend anything between 0~1

		local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness

		local X = TargetAngleX - delta.y
		local Y = TargetAngleY - delta.x
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
		TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y

		AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
		AngleY = AngleY + (TargetAngleY - AngleY) * 0.15

		camera.CFrame = CFrame.new(head.Position, target)
			* CFrame.Angles(0, math.rad(AngleY), 0)
			* CFrame.Angles(math.rad(AngleX), 0, 0)
	end
end

local function InputBegan(input, _gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		MouseClick()
	end
end

local function InputChanged(input, _gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		MouseMoved(input)
	end
end

if UserInputService.MouseEnabled then
	UserInputService.InputBegan:Connect(InputBegan)
	UserInputService.InputChanged:Connect(InputChanged)
end

You can see on line 89 and 90(more specifically in the mouseMoved function, right at the end of it) that * are found. What * does and with what do they help?

1 Like

In this case it’s being used as an operator to add the CFrames together

Usually * is used to multiply 2 numbers but not in the case of when you do:

CFrame.new(1, 0, 0) * CFrame.new(3, 0, 0)

as an example which results in the CFrame value of CFrame.new(4, 0, 0) instead of CFrame.new(3, 0, 0)

It’s traditionally written within 1 line like so:

camera.CFrame = CFrame.new(head.Position, target) * CFrame.Angles(0, math.rad(AngleY), 0) * CFrame.Angles(math.rad(AngleX), 0, 0)

But some people prefer to indent long lines of code to break them up like in Roblox’s script in order to make them more readable, but this is optional really

2 Likes

So those 3 lines could be written in 1 line like you’ve shown?

2 Likes

Yes. It’s just a way of formatting your code so that a line doesn’t get too long.

4 Likes

Ok one more question. You can see just above this:

TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X

How is TargetAngleX which is supposed to hold one value gonna hold two values? For example:

(X >= 80 and 80) 
1 Like

The and expression returns a single value. The left-hand side is just a conditional and the right-hand side is the result of the expression. It is equivalent to the pseudocode if x <= -80 then 80 else nil. If it is nil (condition hasn’t been met), we fall back to X.

3 Likes

So if both “and” conditions return an unexpected value they’re all gonna remain as default X?

1 Like

I wouldn’t say “unexpected”, but, indeed, if both and’s don’t meet their condition, TargetAngleX falls back to X.

3 Likes

Ok, that’s all I could have asked. Thank you for the information you’ve provided. Happy New Year! May 2024 be the best year you’ll ever have!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.