Converting Numbers to Vector3 causes to return 0

Hello, again.
I’m very confused on what is going on here, but here I believe is what’s going on: when I convert 3 numbers into Vector3 to set it as a Part’s size, it returns 0 for some reason instead of Vector3.new(3 Numbers).

Keep in mind there is no error, it’s when converting to Vector3 it returns 0 instead of what it is supposed to return.

Here is the script:

print("Layout now Loading")
local Layout = Http:JSONDecode(DSS:GetAsync("Place1~"..Player.UserId))
for i, v in ipairs(Layout) do
	local NewInstance = Instance.new(Layout[i][1])
	NewInstance.Parent = workspace.GameWorkspace
	NewInstance.Name = Layout[i][2]
	print(tostring(Layout[i][3]), " - "..tostring(Vector3.new(Layout[i][3]))) -- 4th print
	local SizeAsNumber = tonumber(Layout[i][3])
	local Number = Vector3.new(SizeAsNumber) -- This is where I converted
	
	NewInstance.Size = Number
	NewInstance.Position = Vector3.new((Layout[i][4]))
	NewInstance.BrickColor = BrickColor.new(Layout[i][5])
	NewInstance.Material = Layout[i][6]
	NewInstance.Anchored = Layout[i][7]
	NewInstance.Transparency = Layout[i][8]
	print(NewInstance.Size)
end

As you can see below, Layout[i][3] = 100, 1, 100, but once I convert it it returns 0, 0, 0.
(Look at 4th print.)
606c8cbf2dc3ace3e0db6013f00630ba

I’m completely confused as to why this is happening, and any help would be appreciated. Thanks!

I’m not really sure without the code that saves the layout, but the best way to go about storing a vector3 is to store it as a array of 3 values. Also, JSONEncode and decode are used on datastores automatically, you do not need to do it yourself.

Edit: I found your problem. You can’t set a vector3 with a table, you have to unpack it first.

Change this part:

tostring(Vector3.new(Layout[i][3]))

To:

tostring(Vector3.new(unpack(Layout[i][3])))

I had to do it myself because I was saving a dictionary and Encoding myself was the only way to keep an error from occuring.

Oh, then you had a mixed dictionary. In that case, feel free to keep encoding and decoding, I was just trying to clarify. Anywho, I posted a edit above stating a problem I found that is likely the error. You tried to create a Vector3 with a table, which is not how Vector3.new works. You have to unpack the table first.

The following code will not work:

local x = Vector3.new({1,2,3})
--x is Vector3.new(0,0,0)

However, this will: (You have to unpack the table)

local x = Vector3.new(unpack({1,2,3}))
--x is Vector3.new(1,2,3)

Also, to use to number you must call it on each element individually.

I am now getting this error:
945671a06b07f4612043027bc11e1d03
Here is the script

NewInstance.Size = Vector3.new(unpack(Layout[i][3]))

I attempted to put tonumber() on the Layout[i][3] but there was still an error.

It looks like you JSONEncoded a Vector3 and it was converted to a string. This is the main problem with using JSONEncode, since json doesn’t really support custom Roblox types. Is there any reason you need to use a mixed table (both numeric and string indexes)?

1 Like

I’m confused. Here is the table before Encoding it.
Keep in mind the line that says Vector3 is not the line that’s causing the issue, it’s the third line which is before it.

local StartingMapFormat = {
	{
		"Part",
		"Baseplate",
		"100, 1, 100",
		Vector3.new(0, -0.5, 0),
		"Medium stone grey",
		"SmoothPlastic",
		true,
		0,
	}
}

then trying to use/manipulate “100, 1, 100”
a string

Vector3.new(“100, 1, 100”) wont work
you need position to be a Vector3 too.

I believe the problem is that Layout[i][3] is a string, so what you are basically doing is Vector3.new(“100, 1, 100”), which prints 0, 0, 0. You might have to use string patterns to find the digits and insert them into a table.

Try these two:

function StringToVector3(input)
	local parts = Split(input, ",")
	return Vector3.new(tonumber(parts[1]),tonumber(parts[2]),tonumber(parts[3]))	
end

function Vector3ToString(input)
	return tostring(input.x).. "," .. tostring(input.y) .. "," .. tostring(input.z)	
end

function Split(s, delimiter)
    local result = {}

	for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end
2 Likes

What is _G.Split? Is it part of a module?

Updated the missing function, sorry :slight_smile:

Can’t you just use string.split?

2 Likes