How to read errors?

workspace.part.CFrame = workspace.part2.CFrame.LookVector*2

11:54:40.044 - Workspace.Script:1: bad argument #3 (CFrame expected, got Vector3)

What does this error mean and “bad argument #3”?

I am not sure why Roblox says bad argument #3, when there is only one “argument”.

The CFrame property of a Part expects a CFrame value to be given to it, but you have given it a Vector3 value. CFrame.LookVector is a Vector3 value.

workspace.part.Position = workspace.part2.Position + workspace.part2.CFrame.LookVector*2

This will grab its current position, then go 2 studs in front.

This doesn’t mean there are 3 total bad arguments. It means that the third argument is invalid.

I said that. It is confusing because there is no “argument”, as that implies it is a bad argument to a function call however there is no function call here.

(x expected, got y)

this means the script was expecting a X value, but you gave it a Y value. the bad argument #3 should state where the wrong value is, but its kinda hard to read.

Also not really related but i want to fix that code of yours lol

workspace.part.CFrame = CFrame.new(workspace.part.Position, workspace.part2.Position)

The argument in this case is the *2

No that isn’t right.

LookVector is a Vector3 and you can definitely multiply by one by a number.

The argument #3 seems to always be the same.

local box = Instance.new("TextBox")
box.Text = function() end
--  bad argument #3 (string expected, got function)
local p = Instance.new("Part")
p.BrickColor = { }
-- bad argument #3 (BrickColor expected, got table)

Could be a function in the backend that takes the value of the property as the third argument but who knows.

1 Like

I believe the Argument 3 error is from the __newindex metamethod.
__newindex = function( self , key , value )
and the error is caused by the value

That seems to be it. That was what I had in mind however I wasn’t 100% certain.

On a basic level, something along the lines of bad argument #3 basically means that a function was called and that one of the parameters was incorrect. In this case, the 3rd argument was incorrect. This doesn’t really seem relevant to your script of course, because you’re not calling a function in your script, but there’s a technical reason behind this. It’s somewhat in-depth but I’ll explain it below for those who are interested.

In your specific case, you’re getting the error because you’re trying to set the CFrame of a part to a Vector3, and Roblox doesn’t know how to convert a Vector3 into a CFrame.

The in-depth technical stuff

On Roblox, all instances are userdata objects that have metatables set on them. Userdata is a data type provided by Lua to create custom classes in the scripting language, and the metatable defines how data on that custom class can be accessed. Getting and setting properties, getting methods and even defining what values are read-only is controlled via these metatables. Below is an example of what such a metatable looks like:

local metatable = {
	--This function is called whenever you try to read a value from an object
	__index = function(object, valueName)
		print("Reading value: " .. valueName)
		return object[valueName]
	end;

	--This function is called whenever you try to set a value on an object
	__newindex = function(object, valueName, value)
		print("Trying to set value " .. valueName .. " to " .. tostring(value))

		--Catch some error cases here; I've not included that code in this example
		object[valueName] = value
	end;
}

The metatable contains what is called “meta-methods”. There’s a few of these, but for now we’ll focus on __index and __newindex.

__index is called whenever you try to read a value from an object, such as local position = part.Position. In this example, __index would be called with part as the first argument and the string "Position" as the second.

__newindex is called whenever you try to set a value on an object, such as part.Size = Vector3.new(1, 1, 1). In this example, __newindex would be called with part as the first argument, the string "Size" as the second, and the Vector3 1, 1, 1 as the third. However, Roblox internally makes sure that the value you’re trying to set is the correct type, and so you might get that error.

The bad argument #3 is because the third argument in the __newindex metamethod is invalid for the property you’re trying to set.

2 Likes

You try to set a CFrame, but you set a Vector3, this has no logic. And the thing with these error messages is maybe because C++ starts with 0 and not with 1 (remember that if you want to get the first element of a table in lua, you write it like this:

local Table = {"pizza"}
print(Table[1]) --Pizza

But in other programming languages work like this (for example Java):

private string[] Table = {"pizza"}
System.out.print(Table[0]) //Pizza. And yes, I am bad at Java, so if there is any error here, it is because I haven't written in Java for a long time

So the first element is 0, the second 1 and so on.
). Or more simply, I found this a long time ago, it was written by EgoMoose and is a (user defined) class, but it has error messages. Hope this helps. P.S.: To understand it you need to know what are metatables and methadots.