How do i make this more optimized?

I’m making a fangame of the game REx:R but i’m having trouble making the game optimized
Currently all ores postions are stored in a folder as values but i’m wondering how to make them into a table instead so it dosent need to make thousands of objects for no reason

the current code

local val = Instance.new("Vector3Value")
val.Value = newPos
val.Parent = game.ServerStorage.Generated

You can use :GetChildren() to get all the positions stored in the folder. Afterwards, you can use table.insert(table here, value here) to insert the position into the table.

but how would i access the positions from the table afterwards?

for _,generated in pairs(game.ServerStorage.Generated:GetChildren()) do
	if generated.Value == newPos then
		previouslyGenerated = true
	end
end

this is how it checks the postition

Could I see how your explorer is formated, especially the ServerStorage?

image_2023-09-22_193438125
the base layer in serverstorage is just for when the mine resets, and the ores folder is for all the ores that exist, and generated puts vector3values in it and the script checks all of the values to check if it was previously generated, but it starts getting laggy after a bit of mining since it has to check through thousands of values to find 1

You can do table.find(table, value) to find its position
I’m not a great coder but you can use tables like this for it,

local UngeneratedValues = {} -- for all new values, not mined ones
local  GeneratedValues = {} -- for all currently spawned ores

local val = Instance.new("Vector3Value")
val.Value = newPos
val.Parent = game.ServerStorage.Generated
table.insert(UngeneratedValues, val) -- when its made it'll start ungenerated in terms of tables.

for i, generated in pairs(UngeneratedValues:GetChildren()) do
if generated.Value == "newPos" then
previouslyGenerated = true -- idk what this is so i just left it in
table.remove(UnGenerated, generated) -- removing from ungenerated as well, Deleting it so you dont have thousands of unnecessary values. Create more as needed.
table.insert(Generated, generated) -- adding to generated because im assuming newpos will be replaced as this value is *new*
else -- if it DOESNT have its value as "newPos" its put into the "Generated" table, for ores that are generated.
table.insert(Generated, generated)
table.remove(UnGenerated, generated)
end
end

and then if the ore were to be mined you can do table.remove(table, item) or just delete its value.

It’s messy because Im struggling to figure out why youre doing what youre doing
but this is somewhat of how tables work, probably a better way but im going off of what you have so…

  • having 2 tables is a bad idea as if you read the code it already wont work but this is more of an example

well the reason i’m doing it with values is because i used

but modfied it for what i needed

i’m bad at doing advanced scripting so i just used the base of this thing and added lootplan and didnt change the value thing

The _ in your for parameter is quite literally the index, or position in the table.

You forgot about it?

for index,value in ...

well i didn’t make the script :skull: i js modified it