What I'm trying to do is to make a data system that makes player files in Replicated Storage with the data so other scripts can access the data for the player.
Code for getting data (no problem with other stuff for now):
local function dataStoreRetry(dataStoreFunction) local tryCount = 0 local success = false local data = nil repeat tryCount = tryCount + 1 success = pcall(function() data = dataStoreFunction() end) if not success then error("There was an error. Data may not save.") end until tryCount == dataRetries or success return success, data end local function getPlayerData(player) return dataStoreRetry(function() return playerData:GetAsync(player.UserId) end) end
Code for setting up the player file:
local function setupPlayerFile(player) print("Setting up player file...") local success,data = getPlayerData(player) print(success,data) -- All weapons: Equipped weapons: --Data table structure: {Kills,Deaths,[[Weapon,Attachments] ... ],[[Primary,Attachments],[Secondary,Attachments],[Melee]} if success then if data then local plFile = Instance.new("Folder") plFile.Name = player.Name .. " plFile" plFile.Parent = game.ReplicatedStorage["Player Files"] local killCount = Instance.new("IntValue") killCount.Name = "Kills" killCount.Parent = plFile killCount.Value = data[1] local deathCount = Instance.new("IntValue") deathCount.Name = "Deaths" deathCount.Parent = plFile deathCount.Value = data[2] local kdrCount = Instance.new("NumberValue") kdrCount.Name = "KDR" kdrCount.Value = math.ceil(killCount.Value/deathCount.Value) local primaryValue = Instance.new("StringValue") primaryValue.Parent = plFile primaryValue.Name = "PrimaryWeapon" primaryValue.Value = data[4][1][1] local secondaryValue = Instance.new("StringValue") secondaryValue.Name = "SecondaryWeapon" secondaryValue.Parent = plFile secondaryValue.Value = data[4][2][1] local meleeValue = Instance.new("StringValue") meleeValue.Name = "MeleeWeapon" meleeValue.Parent = plFile meleeValue.Value = data[4][3] print("Setup player file.") end if not data then local plFile = Instance.new("Folder") plFile.Name = player.Name .. " plFile" plFile.Parent = game.ReplicatedStorage["Player Files"] local primaryValue = Instance.new("StringValue") primaryValue.Parent = plFile primaryValue.Name = "PrimaryWeapon" primaryValue.Value = "Starter AR" local secondaryValue = Instance.new("StringValue") secondaryValue.Name = "SecondaryWeapon" secondaryValue.Parent = plFile secondaryValue.Value = "Simple Pistol" local meleeValue = Instance.new("StringValue") meleeValue.Name = "MeleeWeapon" meleeValue.Parent = plFile meleeValue.Value = "Knife" print("Setup player file.") end else warn("Data was setup unsuccessfully.") end end
(its not done yet lol)
However, when I run the module script, this happens:
Setting up player file...
true table: 0xc49213c47ee9d245
10:48:33.190 - ReplicatedStorage.PlayerStatManager:84: attempt to index nil with number
10:48:33.195 - Stack Begin
10:48:33.202 - Script 'ReplicatedStorage.PlayerStatManager', Line 84 - function setupPlayerFile
10:48:33.209 - Stack End
The reason it was returning random numbers is because you're returning something that is irrelevant make sure you return a table. Not a function
Here's a fixed version:
-- Function to save player's data local function savePlayerData(playerUserId) if sessionData[playerUserId] then local tries = 0 local success repeat tries = tries + 1 success = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end) if not success then wait(1) end until tries == 3 or success if not success then warn("Cannot save data for player!") end end end
You don't need to return anything since it Will return true.