Referring to a list of variables using 1 function

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    To make one _G function to be able to edit one variable using string.split
  2. What is the issue? Include screenshots / videos if possible!
    I’ve created a datastore script with some variables in it, I want to be able to edit all of the variables attached to the user from any script using a global function. The variables are all called “Item” followed by a number.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried just putting 2 strings together then trying to index it, that doesn’t work, I think I know why. Other than that, I don’t have loads of knowledge of scripting
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- The data I'm attempting to refer to
local function NewData()
	return {
		Item1 = true,
		Item2 = true,
		Item3 = "string"
	}
end

-- How I tried to refer to it
_G.Check = function(Player, ItemNumber)
	local VariableName = "Item"..tostring(ItemNumber)
	if table.find(ServerData[Player], VariableName) then
		return table.find(ServerData[Player], VariableName)
	end
end

I hope this makes sense, I understand this isn’t the best coding, but I have to start somewhere.

What do you want the function to return?

The value of the variable eg. true/false

oh, then

change this to:
return ServerData[Player][table.find(ServerData[Player], VariableName)]

I think the problem is that the function is not actually finding the value in the first place.

This line here:

local VariableName = "Item"..tostring(ItemNumber)

Try this:

_G.Check = function(Player, ItemNumber)
	local VariableName = "Item"..tostring(ItemNumber)
	if ServerData[Player] and ServerData[Player][VariableName] then
		return ServerData[Player][VariableName]
	end
end

table only works for arrays, and from what I can see, this is a dictionary

Thank you, this seems to work.