I am wondering how I’m supposed to iterate through the player’s data table, and then creating a new variable if the player doesn’t have it in their table. Also, I am using Kampfkarren’s DataStore2 Module. I’ve looked through multiple sources in the dev forums, but couldn’t find a solution. I tried to use a for loop (for i, v in pairs
), but after I became confused about what to do next. If you could help me find a solution to this, it would be gladly appreciated! Thank you!
Current DataStore2 Script:
-----------------------
-----------------------
warn(script.Name, "Executing.")
-----------------------
-----------------------
-- MODULES
local dataStoreModule = require(script.DataStoreModule)
-- SERVICES
local playersService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
-- REMOTES
local remotesFolder = replicatedStorage:WaitForChild("Remotes")
local eventsFolder = remotesFolder:WaitForChild("Events")
local updateEvent = eventsFolder:WaitForChild("UpdateEvent")
-----------------------
-----------------------
local function setupPlayerData()
local playerData = {
["Currency"] = {
["Fossil"] = 0;
};
["Tyrannosaurus Rex"] = {
["Owned"] = false;
["Age"] = 0;
["Health"] = 0;
["Hunger"] = 0;
["Thirst"] = 0;
};
["Allosaurus"] = {
["Owned"] = false;
["Age"] = 0;
["Health"] = 0;
["Hunger"] = 0;
["Thirst"] = 0;
};
}
return playerData
end
playersService.PlayerAdded:Connect(function(player)
local playerDataStore = dataStoreModule("PlayerDataStore", player)
local playerData = playerDataStore:Get(setupPlayerData())
local function updateData()
local updatedValue = playerData["Currency"]["Fossil"]
playerDataStore:Set(playerData)
updateEvent:FireClient(player, updatedValue)
end
updateData(playerData)
playerDataStore:OnUpdate(updateData)
end)