How to access a dictionary key with Vector3 values

The script here works without errors, but I can never check to see the type of the block underneath the current one, how can I access the position_pairs using a vector 3 value.

local rp = game:GetService("ReplicatedStorage")
local first = Vector3.new(0,0,0)

local positions = {}
local position_pairs = {}
local values = function(text)
	
end

for a = 1,10 do
	for b = 1, 5 do
		for c = 1,10 do
			table.insert(positions,1,Vector3.new(a*10,b*10,c*10))
		end
	end
end
for i, v in pairs(positions) do
	local random = math.random(1,10)
	if random > 2 and v.Y < 11 then
		position_pairs[v] = "Dirt"
		local part = rp:FindFirstChild("Dirt"):Clone()
		part.Parent = workspace
		part.PrimaryPart = part.Dirt
		part:SetPrimaryPartCFrame(CFrame.new(v))
		continue
	end
	if v.Y < 21 and not position_pairs[v-Vector3.new(0,10,0)] == "Dirt" then
		local part = rp:FindFirstChild("Grass_Block"):Clone()
		part.Parent = workspace
		part.PrimaryPart = part.Grass
		part:SetPrimaryPartCFrame(CFrame.new(v))
	end
	print(position_pairs[tostring(v-Vector3.new(0,10,0))])
end

For starters you are inserting all of the Vector3’s into table position 1, so they are all getting overwritten, replace with table.insert(positions, Vector3.new(a*10, b*10, c*10))

That does not make a difference so far. (same result with or without)

Here you convert to a string before indexing, but everywhere else you do it directly with the Vector3

local positions = {}
local position_pairs = {}

for a = 1,10 do
	for b = 1, 5 do
		for c = 1,10 do
			table.insert(positions,Vector3.new(a*10,b*10,c*10))
		end
	end
end

for i, v in pairs(positions) do
	local random = math.random(1,10)
	
	if random > 2 and v.Y < 11 then
		position_pairs[v] = "Dirt"
	else if v.Y < 21 and not position_pairs[v-Vector3.new(0,10,0)] == "Dirt" then
	    position_pairs[v] = "Grass"
	end
	
	print(position_pairs[v])
end

Let me know if this does what you want

This still does not change anything, The line where I used tostring was a testing line to see if the dictionary stored the key as a string.

This is wrong. table.insert moves all values from pos to #t one index forward, then places value at pos. If no pos is given, it appends value to the table instead.

Sample code:

local t = {}
for i = 1, 10 do
	table.insert(t,1,i)
end
print(t) -- 10, 9, 8, 7, 6 ,5, 4, 3, 2 ,1

Do you mean like this?

local pos = Vector3.new(1, 2, 3)
local value = dict[pos]

Because that doesn’t work.

Vector.new(1, 2, 3) == Vector3.new(1, 2, 3)

evals to true. However they’re only “equal” but not the same object. So when Vector3s are used as dictionary keys you won’t get the same value from the “same” position. E.g.

dict[Vector3.new(1, 2, 3)] = {}
dict[Vector3.new(1, 2, 3)] == dict[Vector3.new(1, 2, 3)] --always false

There are workarounds but I won’t spend a bunch of time typing it out if this isn’t actually what you meant :stuck_out_tongue:

I’m confused at what you’re trying to do.

for a = 1,10 do
	for b = 1, 5 do
		for c = 1,10 do
			table.insert(positions,1,Vector3.new(a*10,b*10,c*10))
		end
	end
end

--// after this your array just looks like this

positions = {
    [1] = Vector3.new(100, 50, 100)
}

Are you asking if you can index a dictionary with Vector3’s?
If so then yes you very much can, The same way you do with nearly every other datatype.
You’re doing it correctly right here

and here

So whats the problem?

You probably haven’t tested your own code. Try this code, it will print true

local dict = {}
dict[Vector3.new(1, 2, 3)] = {}
print(dict[Vector3.new(1, 2, 3)] == dict[Vector3.new(1, 2, 3)])

Vector3, CFrame, and Color3 values are considered same objects if they have same value.
Even this returns true

local dict = {}
dict[CFrame.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)] = {}
print(dict[CFrame.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)] == dict[CFrame.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)])

instead of saving the positions as list why not store them as a dictionary

there is 2 ways you could do this

local blocks = {}

local function ToKey(x, y, z)
    return x .. "," .. y .. "," .. z
end

for x = 1, 10 do
    for y = 1, 5 do
        for z = 1, 10 do
            local key = ToKey(x, y, z)
            blocks[key] = true
        end
    end
end

-- check if the block under 2,4,6 exists
local key = ToKey(2, 4 - 1, 6)
if blocks[key] == nil then
    print("Does not exist")
else
    print("Exists!!!")
end

its also possible to do it with multi dimension dictionary’s

local blocks = {}

local function Set(x, y, z, value)
    if blocks[x] == nil then blocks[x] = {} end -- create the second dimension
    if blocks[x][y] == nil then blocks[x][y] = {} end -- create the third dimension
    blocks[x][y][z] = value
end

local function Get(x, y, z)
    if blocks[x] == nil then return nil end
    if blocks[x][y] == nil then return nil end
    return blocks[x][y][z]
end

for x = 1, 10 do
    for y = 1, 5 do
        for z = 1, 10 do
            Set(x, y, z, true)
        end
    end
end

-- check if the block under 2,4,6 exists
local value = Get(2, 4 - 1, 6)
if value == nil then
    print("Does not exist")
else
    print("Exists!!!")
end

This is what I meant but Ill just rework the system to make it more flexible.