Uhh table.Insert Doesn't Work?

uhh yes…

Hello!

i’m once again asking for your help…
so yes there is this function…well it doesn’t work (i don’t know how to explain it withouth showing it )
i guess here:


local function SpawnBorders(Pos)
	local RandomBorders = math.random(1,#Borders)--Just to choose a random model

	for i,v in pairs(Borders) do--Table of bordermodels
		if i == RandomBorders then
			if v:IsA("Model") then
				local Clone = v:Clone()
				Clone.Parent = workspace
				Clone:PivotTo(CFrame.new(Pos))
				print(Clone.WorldPivot)--Prints vector3
			end
		end
	end
	table.insert(UsedPositions,Pos)--Inserts vector3
	print(UsedPositions.Pos)--Nil?
end

now this function is VERY Important, not only places it down the borders, but it also inserts their position into the usedposition table so that other scripts know not to spawn any other stuff there!
when i went to run this code, it uhh kept placing borders in the same spot… because when i went to check in the table, it never inserted the position apparently?
i even tried instead of the argument inserting the actual world pivot of the model… didn’t do anything.

how do i solve this?
oh! almost forgor :skull: here is the script that checks positions and returns true if they are occupied!

local function CheckIfOccupied(Pos)
	for i,v in pairs(UsedPositions) do
		if v == Position then
			return true
		end
	end
end

thanks in advance!

local function SpawnBorders(Pos)
	local RandomBorders = math.random(1,#Borders)

	for i,v in pairs(Borders) do
		if i == RandomBorders then
			if v:IsA("Model") then
				local Clone = v:Clone()
				Clone.Parent = workspace
				Clone:PivotTo(CFrame.new(Pos))
				print(Clone.WorldPivot)
			end
		end
	end
	UsedPositions["Pos"] = Pos
	print(UsedPositions["Pos"]) -- no longer nil since we added a custom index to it
end

To help explain why your code errors in the way it does:
table.insert is working! but you are indexing it wrong. Try printing the whole table you’ll see something like this:

print(UsedPositions)
--- {
---    [1] = Vector3,
--- }

When using table.insert it will add a new entry to the table at #UsedPositions + 1, this forms a quick to access Array which I think you should stick with considering your lookup function may have to iterate through a lot of used positions it will be faster for the look up to loop through table.insert’d positions.

The currently accepted solution will only store one Position in the UsedPositions table, but with table.insert you can have many.

1 Like

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