Reputation: 5
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 playermanager script/Leaderboard script
local PlayerManager = {}
-- ROBLOX Services
local Players = game.Players
local PointsService = game:GetService('PointsService')
-- Game services
local Configurations = require(game.ServerStorage.Configurations)
local Events = game.ReplicatedStorage.Events
local ResetMouseIcon = Events.ResetMouseIcon
local TeamManager = require(script.Parent.TeamManager)
local DisplayManager = require(script.Parent.DisplayManager)
-- Local variables
local PlayersCanSpawn = false
local GameRunning = false
-- Local Functions
local function OnPlayerAdded(player)
-- Setup leaderboard stats
local leaderstats = Instance.new('Model', player)
leaderstats.Name = 'leaderstats'
local Captures = Instance.new('IntValue', leaderstats)
Captures.Name = 'Captures'
Captures.Value = 0
-- Add player to team
TeamManager:AssignPlayerToTeam(player)
player.CharacterAdded:connect(function(character)
character:WaitForChild('Humanoid').Died:connect(function()
wait(Configurations.RESPAWN_TIME)
if GameRunning then
player:LoadCharacter()
end
end)
end)
-- Check if player should be spawned
if PlayersCanSpawn then
player:LoadCharacter()
else
DisplayManager:StartIntermission(player)
end
end
local function OnPlayerRemoving(player)
TeamManager:RemovePlayer(player)
end
-- Public Functions
function PlayerManager:SetGameRunning(running)
GameRunning = running
end
function PlayerManager:ClearPlayerScores()
for _, player in ipairs(Players:GetPlayers()) do
local leaderstats = player:FindFirstChild('leaderstats')
if leaderstats then
local Captures = leaderstats:FindFirstChild('Captures')
if Captures then
Captures.Value = 0
end
end
end
end
function PlayerManager:LoadPlayers()
for _, player in ipairs(Players:GetPlayers()) do
player:LoadCharacter()
end
end
function PlayerManager:AllowPlayerSpawn(allow)
PlayersCanSpawn = allow
end
function PlayerManager:DestroyPlayers()
for _, player in ipairs(Players:GetPlayers()) do
player.Character:Destroy()
for _, item in ipairs(player.Backpack:GetChildren()) do
item:Destroy()
end
end
ResetMouseIcon:FireAllClients()
end
function PlayerManager:AddPlayerScore(player, score)
player.leaderstats.Captures.Value = player.leaderstats.Captures.Value + score
local success, message = pcall(function() spawn(function() PointsService:AwardPoints(player.userId, score) end) end)
end
-- Event binding
Players.PlayerAdded:connect(OnPlayerAdded)
Players.PlayerRemoving:connect(OnPlayerRemoving)
return PlayerManager
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
Upvotes: 1
Views: 667
Reputation: 26
http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore
You can save data by getting a GlobalDataStore from your game, and use GetAsync of the data store with the player's key when they join, then set the points to that. When they leave the game you can SetAsync to the GlobalDataStore with the player's key. If the player has not saved any data, you can make it 0.
Upvotes: 0