How to connect PlayerStatsManager with Leaderboards (Nil Value Error)? [closed]
Asked by
6 years ago Edited 6 years ago
I'm new to Lua scripting and DataStorage.
I have the PlayerStatsManager Script as Shown (Stored in game.ServerStorage)
(I have also seen similar questions to mine with little/no responses)
002 | local PlayerStatManager = { } |
005 | local DataStoreService = game:GetService( 'DataStoreService' ) |
006 | local playerData = DataStoreService:GetDataStore( 'PlayerData' ) |
009 | local AUTOSAVE_INTERVAL = 60 |
013 | local DATASTORE_RETRIES = 3 |
016 | local sessionData = { } |
020 | function PlayerStatManager:ChangeStat(player, statName, childStat, changeValue) |
021 | sessionData [ player ] [ statName ] [ childStat ] = sessionData [ player ] [ statName ] [ childStat ] + changeValue |
024 | function PlayerStatManager:SetStat(player, statName, childStat, newValue) |
025 | sessionData [ player ] [ statName ] [ childStat ] = newValue |
028 | function PlayerStatManager:GetData(player,statName,childStat) |
029 | return (sessionData [ player ] [ statName ] [ childStat ] ) |
034 | local function dataStoreRetry(dataStoreFunction) |
040 | success = pcall ( function () data = dataStoreFunction() end ) |
041 | if not success then wait( 1 ) end |
042 | until tries = = DATASTORE_RETRIES or success |
044 | error ( 'Could not access DataStore! Warn players that their data might not get saved!' ) |
050 | local function getPlayerData(player) |
051 | return dataStoreRetry( function () |
052 | return playerData:GetAsync(player.UserId) |
057 | local function savePlayerData(player) |
058 | if sessionData [ player ] then |
059 | return dataStoreRetry( function () |
060 | return playerData:SetAsync(player.UserId, sessionData [ player ] ) |
068 | local function setupPlayerData(player) |
069 | local success, data = getPlayerData(player) |
072 | sessionData [ player ] = false |
077 | sessionData [ player ] = { } |
078 | sessionData [ player ] = { Coins = 10 , Escapes = 0 , Vip = false } |
079 | savePlayerData(player) |
082 | sessionData [ player ] = data |
088 | local function autosave() |
089 | while wait(AUTOSAVE_INTERVAL) do |
090 | for player, data in pairs (sessionData) do |
091 | savePlayerData(player) |
097 | game.Players.PlayerAdded:connect(setupPlayerData) |
101 | game.Players.PlayerRemoving:connect( function (player) |
102 | savePlayerData(player) |
103 | sessionData [ player ] = nil |
110 | return PlayerStatManager |
And I'm trying to call that script inside of my leaderboard script, located in game>Workspace>Main>Stats. This is what I have so far, and I'm attempting to reach it by calling Wins.Value = PlayerStatManager:GetData(Player, "Wins", 0)
01 | PlayerStatManager = require(game.ServerStorage.PlayerStatManager) |
03 | game.Players.PlayerAdded:connect( function (Player) |
06 | local Folder = Instance.new( "Folder" , Player) |
07 | Folder.Name = 'leaderstats' |
09 | local settingFolder = Instance.new( "Folder" , Player) |
10 | settingFolder.Name = 'PlayerSettings' |
13 | local Playing = Instance.new( 'BoolValue' , Player) |
14 | Playing.Name = 'Playing' |
16 | local isAFK = Instance.new( 'BoolValue' , Player) |
21 | local Escapes = Instance.new( 'IntValue' , Folder) |
23 | Wins.Value = PlayerStatManager:GetData(Player, "Wins" , 0 ) |
26 | local Coins = Instance.new( 'IntValue' , Folder) |
28 | Coins.Value = PlayerStatManager:GetData(Player, "Coins" , 10 ) |
32 | local Kills = Instance.new( "IntValue" , settingFolder) |
I'm new to DataStorage, and keep getting nil value errors. The most recent error reads
18:55:55.154 - ServerStorage.PlayerStatManager:29: attempt to index field '?' (a nil value)
I've toggled with this script a little and get different errors, but they all result in this sort of error.
I'm just trying to save and store data right now. Can't get passed this error