Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Leaderboard statistics not showing, why?

I've been trying to make a deaths/kills leaderboard and save the data, but everytime I add anything about DataStoreService or just data saving, the leaderboard only shows the names of players but not kills or deaths. I've tried it in game and in studio and still doesn't work.

My code is compiled from different articles because I'm still quite new to Lua(I can code well in other languages). Anyways, here is my code, any help will be appreciated:

local DataStoreService = game.GetService("DataStoreService")
local killsDataStore = DataStoreService:GetDataStore("KOstats")

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	 
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	 
	local kills = Instance.new("IntValue")
	kills.Name = "KOs"
	kills.Parent = leaderstats
	
	local data
	local success,errormessage = pcall(function() --Message for success or error
        data = killsDataStore:GetAsync(player.UserId.."deaths") 

    end)
    if success then --Checks if data is successfully loaded
       kills.Value = data
    else
        print("There was an error while getting data")
        warn(errormessage) --Warn is like print, but in yellow
    end
	
	 
	player.CharacterAdded:Connect(function(character)
	    character:WaitForChild("Humanoid").Died:Connect(function()
	        deaths.Value = deaths.Value + 1
	    end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player) -- When player is removed do
    local success,errormessage = pcall(function()
        killsDataStore:SetAsync(player.UserId.."deaths",player.leaderstats.kills.Value)
    end)
    if success then
        print("Data successfully saved!")
    else
        print("There was an error when saving data!")
        warn(errormessage)
    end
end)

1 answer

Try my script, I have written comments to guide you through.

local Players = game:GetService("Players") -- Reference the Service "Players"
local DataStoreService = game:GetService("DataStoreService") -- Reference the Service "DataStoreService"
local DeathsDataStore = DataStoreService:GetDataStore('Deaths') -- Get the DataStore for Deaths
local kOsDataStore = DataStoreService:GetDataStore('kOs') -- Get the DataStore for Deaths
Players.PlayerAdded:Connect(function(plr) -- Establish a RBXScriptSignal for the PlayerAdded Event
	local deaths -- Create the variable "deaths"
	local DataSuccess, DataErr = pcall(function() -- Establish a pcall to wrap errors
		deaths = DeathsDataStore:GetAsync(plr.UserId .. "-data") -- Get the data from the API
	end)
	if DataSuccess then -- if Data was sucessfully got then
		print('Success on getting player data for ', plr.Name)
		if deaths == nil then -- Check if data is nil
			deaths = "0" -- set deaths to 0
		end
	else
		deaths = "0" -- set deaths to 0
	end
	deaths = tonumber(deaths) -- set deaths to the tonumber deaths.
	local kOs -- Create the variable "kOs"
	DataSuccess, DataErr = pcall(function() -- Establish a pcall to wrap errors
		kOs = DeathsDataStore:GetAsync(plr.UserId .. "-data") -- Get the data from the API
	end)
	if DataSuccess then -- if Data was sucessfully got then
		print('Success on getting player data for ', plr.Name)
		if kOs == nil then -- Check if data is nil
			kOs = "0" -- set deaths to 0
		end
	else
		kOs = "0" -- set deaths to 0
	end
	kOs = tonumber(kOs) -- set deaths to the tonumber deaths.
	local leaderstats = Instance.new("Folder", plr) -- Create the Instance "Folder" in the player
	leaderstats.Name = "leaderstats" -- set the leaderstats variable name to "leaderstats"
	local DeathsInt = Instance.new("IntValue", leaderstats) -- Create the Instance "IntValue" in the leaderstats
	DeathsInt.Name = "Deaths" -- set the DeathsInt variable name to "Deaths"
	local kOsInt = Instance.new("IntValue", leaderstats) -- Create the Instance "IntValue" in the leaderstats
	kOsInt.Name = "KOs" -- set the kOsInt variable name to "KOs"
	kOsInt.Value = kOs -- set the kOsInt's value to the variable kOs
	DeathsInt.Value = deaths -- set the DeathsInt's value to the variable deaths
	plr.CharacterAdded:Connect(function(char) -- Establish a RBXScriptSignal for the Player.CharacterAdded Event
		local humanoid = char:WaitForChild("Humanoid")
		humanoid.Died:Wait() -- Yield the thread until the humanoid dies.
		DeathsInt.Value = DeathsInt.Value + 1
	end)
end)
Players.PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function() -- Establish a pcall to wrap errors
		DeathsDataStore:SetAsync(plr.UserId.."-data", tostring(plr.leaderstats.Deaths.Value)) -- Save the data to the API
		kOsDataStore:SetAsync(plr.UserId.."-data", tostring(plr.leaderstats.KOs.Value)) -- Save the data to the API
	end)
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.