How do I save player data e.g amount of points on a leaderboard?
Asked by
7 years ago Edited 7 years ago
How do I save the data on a leaderboard for example, the amount of points so that when the player rejoins, they still have their amount of points they earned previously.
In my game, I made a leaderboard in my playermanager script where I have code on what happens to the player when they join the game(Capture The Flag Game) and what happens when they capture the flag and how it displays on the leaderboard. What do I have to edit in the code to make sure the amount of captures the player makes in the game is saved on the leaderboard so that when they rejoin they have that data of the amount of captures they make still saved on the leaderboard
My Player Manager/Leaderboard Script:
001 | local PlayerManager = { } |
004 | local Players = game.Players |
005 | local PointsService = game:GetService( 'PointsService' ) |
008 | local Configurations = require(game.ServerStorage.Configurations) |
009 | local Events = game.ReplicatedStorage.Events |
010 | local ResetMouseIcon = Events.ResetMouseIcon |
011 | local TeamManager = require(script.Parent.TeamManager) |
012 | local DisplayManager = require(script.Parent.DisplayManager) |
015 | local PlayersCanSpawn = false |
016 | local GameRunning = false |
019 | local function OnPlayerAdded(player) |
021 | local leaderstats = Instance.new( 'Model' , player) |
022 | leaderstats.Name = 'leaderstats' |
024 | local Captures = Instance.new( 'IntValue' , leaderstats) |
025 | Captures.Name = 'Captures' |
029 | TeamManager:AssignPlayerToTeam(player) |
031 | player.CharacterAdded:connect( function (character) |
032 | character:WaitForChild( 'Humanoid' ).Died:connect( function () |
033 | wait(Configurations.RESPAWN_TIME) |
035 | player:LoadCharacter() |
041 | if PlayersCanSpawn then |
042 | player:LoadCharacter() |
044 | DisplayManager:StartIntermission(player) |
049 | local function OnPlayerRemoving(player) |
050 | TeamManager:RemovePlayer(player) |
055 | function PlayerManager:SetGameRunning(running) |
056 | GameRunning = running |
059 | function PlayerManager:ClearPlayerScores() |
060 | for _, player in ipairs (Players:GetPlayers()) do |
061 | local leaderstats = player:FindFirstChild( 'leaderstats' ) |
063 | local Captures = leaderstats:FindFirstChild( 'Captures' ) |
071 | function PlayerManager:LoadPlayers() |
072 | for _, player in ipairs (Players:GetPlayers()) do |
073 | player:LoadCharacter() |
077 | function PlayerManager:AllowPlayerSpawn(allow) |
078 | PlayersCanSpawn = allow |
081 | function PlayerManager:DestroyPlayers() |
082 | for _, player in ipairs (Players:GetPlayers()) do |
083 | player.Character:Destroy() |
084 | for _, item in ipairs (player.Backpack:GetChildren()) do |
088 | ResetMouseIcon:FireAllClients() |
091 | function PlayerManager:AddPlayerScore(player, score) |
092 | player.leaderstats.Captures.Value = player.leaderstats.Captures.Value + score |
093 | local success, message = pcall ( function () spawn( function () PointsService:AwardPoints(player.userId, score) end ) end ) |
097 | Players.PlayerAdded:connect(OnPlayerAdded) |
098 | Players.PlayerRemoving:connect(OnPlayerRemoving) |
My datastore
My datastore script:
01 | local DSService = game:GetService( 'DataStoreService' ):GetDataStore( 'Hamburger223232' ) |
02 | game.Players.PlayerAdded:connect( function (plr) |
04 | local uniquekey = 'id-' ..plr.userId |
05 | local leaderstats = Instance.new( 'IntValue' ,plr) |
06 | local savevalue = Instance.new( 'IntValue' ) |
07 | leaderstats.Name = 'leaderstats' |
08 | savevalue.Parent = leaderstats |
09 | savevalue.Name = 'Captures' |
11 | local GetSaved = DSService:GetAsync(uniquekey) |
13 | savevalue.Value = GetSaved [ 1 ] |
15 | local NumbersForSaving = { savevalue.Value } |
16 | DSService:SetAsync(uniquekey,NumbersForSaving) |
22 | game.Players.PlayerRemoving:connect( function (plr) |
23 | local uniquekey = 'id-' ..plr.userId |
24 | local Savetable = { plr.leaderstats.Captures.Value } |
25 | DSService:SetAsync(uniquekey,Savetable) |
I tried adding a separate datastore on a different script but it didn't work so I am hoping that I could add some code to this script to make sure the amount of captures the player makes is saved on it so that the player can use its captures to buy things in the game etc.
Thank You And Help is Appreciated