I have been having some trouble with DataStore lately, I know it's possible to do it with a leaderboard, but how?
The value I want to save is called Points and it is located in a model called leaderstats inside a player.
Here is the script.
local playerStats = {} --this keeps a list of the stats for each player that enters the game game.Players.PlayerAdded:connect(function(player) --Added function local leaderstats = Instance.new("Model", player) leaderstats.Name = "leaderstats" local Points = Instance.new("StringValue", leaderstats) Points.Name = "Points" Points.Value = 0 local grouprank = Instance.new("StringValue", leaderstats) grouprank.Name = "Rank" local rank = player:GetRoleInGroup(1066846) local rankn = player:GetRankInGroup(1066846) grouprank.Value = rank if rankn == 75 then local team = game:GetService("Teams") player.TeamColor = team.DJ.TeamColor playerStats[player] = leaderstats end end) --[[ -refrences- leaderstats Model inside the player holding the values. Points String value in the leaderstats model Rank String value in the leaderstats model --]]
Well the 2 prime ways of saving and loading data with DataStore would be: GetAsync for retrieving data UpdateAsync for saving data
All DataStore events/methods must be called on the datastore service so it is recommended you define that as a variable straight away. Then, when the player joins the game, you can use a ternary operator with GetAsync to set values.
As for when a player leaves the game, you can use UpdateAsync to save the values. BUT I recommend you add a game.OnClose function for when the last player leaves the server because it doesn't always save. Here is an example with the leaderstat you provided:
ds = game:GetService("DataStoreService"):GetDataStore("Points") -- GetDataStore provides a key to save everything under. Can be anything. game.Players.PlayerAdded:connect(function(player) local leaderstats = Instance.new("IntValue",player) -- Creating the leaderboard leaderstats.Name = "leaderstats" local points = Instance.new("IntValue",player) points.Name = "Points" points.Value = ds:GetAsync(player.Name) or 0 -- Here we look for the players name under "Points". DataStores act like a table, and if the players name doesn't yet exist, it sets the points to 0 by default. end) game.Players.PlayerRemoving:connect(function(player) ds:UpdateAsync(pplayer.Name, function(oldValue) return p.leaderstats.Points.Value end) -- This can be confusing. The first argument, p.Name, is the name of the key inside the Points DataStore. The value you update it with has to be a function, so function(oldValue) return value end is the recommended way to go. The argument of the function is what the value used to be end) game.OnClose = function() -- This part is recommended to ensure the game saves when the last player leaves the server before it shuts down! for i,v in pairs(game.Players:GetChildren()) do ds:UpdateAsync(v.Name, function(oldValue) return v.leaderstats.Points.Value end) end wait(1) end)
For any further help contact me on ROBLOX at Jonibus or look up Data Store on the ROBLOX wiki!