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

How do I save player data e.g amount of points on a leaderboard?

Asked by 6 years ago
Edited 6 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:

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

My datastore My datastore script:

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)

    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue',plr)
    local savevalue = Instance.new('IntValue')
    leaderstats.Name = 'leaderstats'
    savevalue.Parent = leaderstats
    savevalue.Name = 'Captures'

    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        DSService:SetAsync(uniquekey,NumbersForSaving)


    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = 'id-'..plr.userId
    local Savetable = {plr.leaderstats.Captures.Value}
    DSService:SetAsync(uniquekey,Savetable)
end)

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

0
I can see how you attempted to use AlvinBloxx's code Lmao, But I don't see any ways how this won't work, the script looks fine greatneil80 2647 — 6y
0
Only the first script is confusing greatneil80 2647 — 6y
0
OK aspiringstar346 8 — 6y

Answer this question