Hey all,
Pretty new to roblox coding. Took a few introductory classes in HS and college, but it's been a few years since I've done any actual coding. Very interested in learning though, so if anyone has a solution to my problem, please let me know!
I'm having some problems with datastores. I'm using the datastore that was used in the Datastores tutorial, and I can get my data to increment, but when trying to access the data in other scripts, I can't seem to make it work. Currently trying to get a value from my datastore to show on the leaderboard. Pretty sure the issue is me not fully understanding tables. I can often return the player and the value, but I can't get the value alone to return.
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.
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)
while(not (sessionData[player] and sessionData[player][statName]))do
wait(1)
end
sessionData[player][statName] = sessionData[player][statName] + changeValue
print("Incremented",statName,"by",changeValue,"for",player)
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 PlayerStatManager:getPlayerData(player, statName)
return dataStoreRetry(function()
return sessionData[player][statName]
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)
print("Setting up data for",player)
local success, data = getPlayerData(player)
print(player,success,data)
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] = {Money = 0, Experience = 0, BlueEssence = 0}
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
for _,plr in pairs(game.Players:GetPlayers())do
setupPlayerData(plr)
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
local PlayerStatManager = require(game.ServerStorage.PlayerStatManager)
-- After player joins we'll periodically give the player money and experience
game.Players.PlayerAdded:connect(function(player)
local gamer = PlayerStatManager:getPlayerData(player, 'Money')
print (gamer)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = PlayerStatManager:getPlayerData(player, 'Money')
money.Parent = leaderstats
PlayerStatManager:ChangeStat(player, 'Money', 5)
PlayerStatManager:ChangeStat(player, 'Experience', 1)
print("ok")
end)
