I'm trying to use a loop to save multiple number values to a player when they join the game. The keys table contains multiple tables. Each sub table has two values: the data persistence key and the instance name. Basically, when a player joins the game, I want the keys table to be looped through. If a number value is found with the name of v[2], then it's value should be loaded as v[1] (the key). Every five seconds, I want all players to be looped through, and have their data be saved. I'm not sure if I'm even close to being right with this, but I figured I'd give it a shot. The output isn't saying anything is wrong, by the way.
local keys = {
{"cpc", "CPC"},
{"cookies", "Cookies"},
{"cps", "CPS"}
}
game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady()
local location = player.PlayerGui.CookieClicker.Data
for i, v in pairs(keys) do
pcall(function()
if location:FindFirstChild(v[2]) then
location[v[2]].Value = player:LoadNumber(v[1]) or 0
end
end)
end
end)
while wait(5) do
for _, player in pairs(game.Players:GetPlayers()) do
local location = player.PlayerGui.CookieClicker.Data
for i, v in pairs(keys) do
pcall(function()
if location:FindFirstChild(v[2]) then
player:SaveNumber(v[1], location[v[2]].Value)
end
end)
end
end
end

2 comments
Typo: I'm trying to use a loop to load* multiple number values to a player when they join the game.
May I reformat this in DataStore?