Refering to undefined instances in constant tables

So the title might not make much sense but basically I have a table of code for data:

handler.leaderboards = {
	['MostRolled'] = {
		Part = leaderboards.MostRolls.Plane,
		Datastore = dataStoreService:GetOrderedDataStore("MostRolled"),
		ValuePrefix = "Rolls",
		Path = "{plr}.leaderstats.Rolls.Value"
	}
}

I want to call this table later and concatenate the player instance to the path value but I’m not sure how to do that without getting an error. Here is a snippet of where I’m using it.

local function saveAll(lbInfo)
	local datastore:OrderedDataStore = lbInfo.Datastore
	
	for _, plr in pairs(players:GetPlayers()) do
		print(players[lbInfo.Path:gsub("{plr}", plr.Name)].Value)
		task.wait(0.5)
	end
end

I knew this wouldn’t work in the first place, but for the sake of an example I did it anyway.
Heres the error I get whenever I run this:

CaelmDev.leaderstats.Rolls.Value is not a valid member of Players "Players"

Hopefully I made it clear enough. If you can help me please do thanks.

You will need to split the path into its components, and reference each component one by one. Something like this should work:

local pathString = lbInfo.Path:gsub("{plr}", plr.Name)
local path = string.split(pathString, ".")

local object = players
for i, childName in ipairs(path) do
    object = object[childName]
end

print(object.Value)
1 Like

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