Is this correct for a kill/death leaderboard that saves?

this is for a kills and death leaderboard that saves when a player leaves the game and when they come back they have the stats

Provide an overview of:

  • What does the code do and what are you not satisfied with? im not sure if i got it right
  • What potential improvements have you considered? aksing for some help
  • How (specifically) do you want to improve the code? fix all the bugs and or errors
--  blocklocal deathsStore = game:GetService("DataStoreService"):GetDataStore("Deaths")

function playerAddedHandler(plr)
    -- key that is used to store the death value
    local playerKey = "Player_" .. plr.UserId 

    -- Add leader board
    local leaderstats = Instance.new("Folder", plr)
    leaderstats.Name = "leaderstats"

    -- Add "Deaths" column to leader board 
    local deaths = Instance.new("IntValue", leaderstats)
    deaths.Name = "Deaths"

    -- Load Deaths value from previous game
    deaths.Value = deathsStore:GetAsync(playerKey) 

    -- Hook up event handler that is triggered when character dies
    plr.CharacterAdded:Connect(function(char)
        char.Humanoid.Died:Connect(function()

            -- increment death value on the leader board
            plr.leaderstats.Deaths.Value = plr.leaderstats.Deaths.Value + 1

            -- save the value in our deaths datastore
            local success, err = pcall(function()
                deathsStore:SetAsync(playerKey, plr.leaderstats.Deaths.Value)
            end)
        end)
    end)
end

game.Players.PlayerAdded:Connect(playerAddedHandler) -- call function "playerAddedHandler" every time a player joins the game 
2 Likes

Besides nitpicks, there are two things you are doing “incorrectly”.

Aim to only save when the player isn’t connected to the session anymore. The two most common points to execute a full save are via PlayerRemoving and BindToClose.

1 Like

i dont understand how to fix it at all i just made it and dont know what to edit even with those links

1 Like

They’re reading materials. You can use these as research points and link them to the comments I’ve made to get a better understanding of why what I pointed out is incorrect. I’ve also given some pointers about what steps you can take to fix the issues.

You can do further API research via the Developer Hub if you need additional help or reference. Feel free to look into it a bit and make a fix attempt.

1 Like

ok thank you that sounds good have a good day

1 Like