Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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)

-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}

-- Create variable for the DataStore.
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

-- Create variable to configure how often the game autosaves the player data.
local AUTOSAVE_INTERVAL = 60

-- Number of times we can retry accessing a DataStore before we give up and create
-- an error.
local DATASTORE_RETRIES = 3

-- Table to hold all of the player information for the current session.
local sessionData = {}

-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, childStat, changeValue)
    sessionData[player][statName][childStat] = sessionData[player][statName][childStat] + changeValue
end

function PlayerStatManager:SetStat(player, statName, childStat, newValue)
    sessionData[player][statName][childStat] = newValue
end

function PlayerStatManager:GetData(player,statName,childStat)
    return (sessionData[player][statName][childStat])
end

-- Function to retry the passed in function several times. If the passed in function
-- is unable to be run then this function returns false and creates an error.
local function dataStoreRetry(dataStoreFunction)
    local tries = 0 
    local success = true
    local data = nil
    repeat
        tries = tries + 1
        success = pcall(function() data = dataStoreFunction() end)
        if not success then wait(1) end
    until tries == DATASTORE_RETRIES or success
    if not success then
        error('Could not access DataStore! Warn players that their data might not get saved!')
    end
    return success, data
end

-- Function to retrieve player's data from the DataStore.
local function getPlayerData(player)
    return dataStoreRetry(function()
        return playerData:GetAsync(player.UserId)
    end)
end

-- Function to save player's data to the DataStore.
local function savePlayerData(player)
    if sessionData[player] then
        return dataStoreRetry(function()
            return playerData:SetAsync(player.UserId, sessionData[player])
        end)
    end
end

-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.
local function setupPlayerData(player)
    local success, data = getPlayerData(player)
    if not success then
        -- Could not access DataStore, set session data for player to false.
        sessionData[player] = false
    else
        if not data then
            print"what lol"
            -- DataStores are working, but no data for this player
            sessionData[player] = {}
            sessionData[player] = {Coins = 10, Escapes = 0, Vip = false}
            savePlayerData(player)
        else
            -- DataStores are working and we got data for this player
            sessionData[player] = data
        end
    end 
end

-- Function to run in the background to periodically save player's data.
local function autosave()
    while wait(AUTOSAVE_INTERVAL) do
        for player, data in pairs(sessionData) do
            savePlayerData(player)
        end
    end
end

-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)

-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
    savePlayerData(player)
    sessionData[player] = nil
end)

-- Start running autosave function in the background.
spawn(autosave)

-- Return the PlayerStatManager table to external scripts can access it.
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)

PlayerStatManager = require(game.ServerStorage.PlayerStatManager)

game.Players.PlayerAdded:connect(function(Player)

    -- Folders
    local Folder = Instance.new("Folder", Player)
    Folder.Name = 'leaderstats'

    local settingFolder = Instance.new("Folder", Player)
    settingFolder.Name = 'PlayerSettings'

    -- Player
    local Playing = Instance.new('BoolValue', Player)
    Playing.Name = 'Playing'
    Playing.Value = false
    local isAFK = Instance.new('BoolValue', Player)
    isAFK.Name = 'isAFK'
    isAFK.Value = false

    -- Leaderboard
    local Escapes = Instance.new('IntValue', Folder)
    Wins.Name = 'Wins'
    Wins.Value = PlayerStatManager:GetData(Player, "Wins", 0)
    print(Wins.Value)

    local Coins = Instance.new('IntValue', Folder)
    Coins.Name = 'Coins'
    Coins.Value = PlayerStatManager:GetData(Player, "Coins", 10)
    print(Coins.Value)

    -- In-game Leaderboard
    local Kills = Instance.new("IntValue", settingFolder)
    Kills.Name = "Kills"
    Kills.Value = 0

end)

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

Closed as Not Constructive by User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?