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 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 =[i][1])
	NewInstance.Parent = workspace.GameWorkspace
	NewInstance.Name = Layout[i][2]
	print(tostring(Layout[i][3]), " - "..tostring([i][3]))) -- 4th print
	local SizeAsNumber = tonumber(Layout[i][3])
	local Number = -- This is where I converted
	NewInstance.Size = Number
	NewInstance.Position =[i][4]))
	NewInstance.BrickColor =[i][5])
	NewInstance.Material = Layout[i][6]
	NewInstance.Anchored = Layout[i][7]
	NewInstance.Transparency = Layout[i][8]

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.)

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:




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 works. You have to unpack the table first.

The following code will not work:

local x ={1,2,3})
--x is,0,0)

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

local x ={1,2,3}))
--x is,2,3)

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

I am now getting this error:
Here is the script

NewInstance.Size =[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)?

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 = {
		"100, 1, 100",, -0.5, 0),
		"Medium stone grey",

then trying to use/manipulate “100, 1, 100”
a string“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“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, ",")

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

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

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

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

Updated the missing function, sorry :slight_smile:

Can’t you just use string.split?

1 Like