What's the best way to handle the data for a forest of choppable trees?

I’m trying to make an axe that can chop down a tree that was created by the server.

I’ve got everything all set-up and ready to go with all that FilteringEnabled stuff. I just need help handling the data for a forest full of trees.

So, when handling the data of all trees (health, drops, etc), should that be stored in Value objects (Int, Object, String) inside each tree itself? --But wouldn’t this mean the client has ‘access’ to the tree data and could abuse it, like making it a 1-hit chop down and dropping extra wood?

Or should I set up a datatable to handle all the tree data and fill in values as the trees spawn in? --This feels like it could be more work for the server, since there could be 100,000 or more trees at once, but I really don’t know enough about the limitations of tables yet. Plus I have no idea how this works across multiple servers running at once.

If anyone can point me in the right direction, would be greatly appreciated!

So this is a classic case where you could hold the data either in a table stored in some server / modulescript or you could use object oriented programming which allows you to essentially create A object just like any other roblox built it in object such as a Part which has parameters such as Transparency, Material, etc. Now fortunately lua does have the ability for you to create your own objects and there are many devforum articles which teach you how to utilize object oriented programming here are some good ones so you don’t need to do too much searching. :slight_smile:

I really do help this helps.

OOP also makes it easy since it allows you to create your own methods, usually in all of my objects I have things like a disconnect method, destroy method which just allows me to remove the object when I am done using it whilst disconnecting any connections that need to be disconnected to prevent memory leaks.

1 Like

@BostonWhaler Awesome thanks! I will look into this.

1 Like

Okay, so after panning through those links, I think I might already be sort of doing this.

I have a ModuleScript that handles how my objects spawn that starts out like this…

ServerStorage = game:GetService("ServerStorage")
objj = ServerStorage.Objects

objects = {
	["PineTree"] = {
		["Name"] = "Pine Tree",
		["Type"] = "Tree",
		["Model"] = objj.Tree_Pine,
		["Size"] = "currently unused",
		["Rotation"] = math.random(0,360),
		["HMin"] = 0,
		["HMax"] = 0,
		["AMin"] = 0,
		["AMax"] = 0,
		["ColorsA"] = {"Earth green", "Sea green", "Dark green", "Parsley green"},
		["ColorsB"] = {"Brown1", "Brown2"},

And continues with a bunch of different objects.

And then they are spawned via a function inside the same ModuleScript…

function objects.spawn(obj,qty)

Which is then accessed via a separate ServerScript…

local myFunction = require(workspace.LEARNModScript_ModuleScript)

function spawnForestObjects()
	print("spawning forest objects...")


(I may have deleted some important code while I was reducing this example down for clarity, but it does seem to work exactly as it is supposed to.)

And then all spawned objects are stuck inside a Folder inside the workspace called SpawnedObjects.

So, I could easily add data for health and item drops in the dictionary.
But then how would I handle what happens if a player chops down the 319th ‘Pine Tree’ that was spawned?

Do I generate a Dictionary out of the SpawnedObjects Folder and edit the values as players interact with my trees? How does this work with multiple servers? Does each server create it’s own separate dictionary?