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

Wiki Datastore Save/Load Module Not Working, Can't Access Datastore?

Asked by 6 years ago
Edited 6 years ago

I'm using the Wiki's Datastore Module to save data in my game, and it worked in a previous game I made but in this one it's not working. It keeps throwing the error "Could not access DataStore! Warn players that their data might not get saved!" Why is this?

Please note I AM testing this in servers.

The full error is

12:12:32.645 - ServerStorage.Data:49: Could not access DataStore! Warn players that their data might not get saved!
12:12:32.646 - Stack Begin
12:12:32.649 - Script 'ServerStorage.Data', Line 49 
12:12:32.650 - Script 'ServerStorage.Data', Line 64 - upvalue savePlayerData
12:12:32.652 - Script 'ServerStorage.Data', Line 82
12:12:32.654 - Stack End
-- 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('TESTINGDATA3')

-- 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, changeValue)
    sessionData[player][statName] = sessionData[player][statName] + changeValue
end

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

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

function PlayerStatManager:AddTable(player, statName, addition)
    table.insert(sessionData[player][statName], addition)
    print("Added "..addition)
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
            -- DataStores are working, but no data for this player
            sessionData[player] = {FirstTimePlaying = true, House="Starter", Money=500, HairColour=(Color3.fromRGB(0, 0, 0)), EyeColour="rbxassetid://"..1606092452, Gender="Male", Car="None", Shirt="White Tee", PantsW="Black Jeans", Hair="None", Hat="None", Shirts = {}, Pants = {}, Hats = {}}
            savePlayerData(player)
            print(player.Name.." has been given first-time stats")
        else
            -- DataStores are working and we got data for this player
            sessionData[player] = data
            print(player.Name.." has been given back their stats")
        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
0
DataStore don`t work in Roblox Studio, you must be on a server Leamir 3138 — 6y
0
That's where the error is being thrown, in servers. BouncingBrenda 44 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Hey everyone! I fixed the problem. Apparently you can't save a color3 value in the datastore I guess.

Ad

Answer this question