How do i save stats on my leaderboard?

I have some issues with my leaderboard…
I want a leaderboard with Coin or money, kills and death.
I want all stats to save.
and i want the player to get 10 coins/money per 10 seconds.
i have tried to add some saving script but it always mess up my leaderboard. idk what im doing wrong.
Im not a scripter so i dont really understand it. i just watch yt…
thanks for your time and help <3

2 Likes

game.Players.PlayerAdded:Connect(function(plr)

local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
stats.Parent = plr

local Coins = Instance.new(“IntValue”)
Coins.Name = “Coins”
Coins.Parent = stats

local kills = Instance.new("IntValue")

kills.Name = “Kills”
kills.Parent = stats

local deaths = Instance.new(“IntValue”)
deaths.Name = “Deaths”
deaths.Parent = stats

plr.CharacterAdded:connect(function(char)
local humanoid
repeat
humanoid = char:FindFirstChild(“Humanoid”)
wait()
until humanoid
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
local tag = humanoid:FindFirstChild(“creator”)
if tag then
local killer = tag.Value
if killer then
killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
end
end
end)
end)
end)

local amount = 10–amount of coins we will be giving
local timeamount = 10–cooldown time in seconds, change if you want
local currencyname = “Coins”–name of our currency, change to your currency name

while true do-- while this is true do
wait(timeamount)-- wait for the amount of time we put in the timeamount variable
for i,v in pairs(game.Players:GetPlayers()) do–for loop
if v:FindFirstChild(“leaderstats”) then-- if the player finds the leaderstats then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount–adds currency
end
end
end

– in this very specific example it’s saving cash

local Players, DataStoreService = game:GetService(“Players”), game:GetService(“DataStoreService”)
local cash_data_store = DataStoreService:GetDataStore(“CashDataStore”)

local function load(player: Player)
– …
end

local function save(player: Player)
– …
end

Players.PlayerAdded:Connect(function(player: Player)
load(player)
end)

Players.PlayerRemoving:Connect(function(player: Player)
save(player)
end)

game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
save(player)
end
end)

1 Like

Create this leaderstats script

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats

        local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
        local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats

	while wait(1) do
		player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += 10
	end
end)

And inside the leaderstats script, add a script and paste the code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")

Players.PlayerAdded:Connect(function(player)
	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
		print("Successfully loaded "..tostring(player.Name)..tostring("'s data."))
	end)

	if success then
		if Data then
			for i, v in pairs(Data) do
				player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
			end
		end
	else
		error(errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
		print("Successfully got "..tostring(player.Name)..tostring("'s data."))
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

The script above saves ALL of the values in the leaderstats.

2 Likes

Where do i create a leaderstat script? under workspace or serverscriptservice?

1 Like

serverscriptservice
workspace is mainly putting parts so its obvious

1 Like

sorry. i have it under workspace now… not sure why hahaha il try it out. thank you <3

1 Like

Always remember that you put leaderstats script in ServerScriptService.
You might need to watch a YT tutorial if you want to get the kills and deaths working.

thank you! the leaderboard works… it used to give the death and kills… so… i will read about it again… sorry. im just a mom thats making a game with my kids :slight_smile:

1 Like

Please remember to copy the save script and put it somewhere safe(the one inside leaderstats)… you will create more games, and you might need to save data on those too!

1 Like

i added this and now it all works :slight_smile: thank you so much for your help

1 Like

Players.PlayerAdded:connect(function(Player)
wait(1)
local Stats = Template:Clone()
Stats.Parent = Player
local Deaths = Stats.Deaths
Player.CharacterAdded:connect(function(Character)
Deaths.Value = Deaths.Value + 1
local Humanoid = Character:FindFirstChild(“Humanoid”)
if Humanoid then
Humanoid.Died:connect(function()
for i, Child in pairs(Humanoid:GetChildren()) do
if Child:IsA(‘ObjectValue’) and Child.Value and Child.Value:IsA(‘Player’) then
local Killer = Child.Value
if Killer:FindFirstChild(‘leaderstats’) and Killer.leaderstats:FindFirstChild(“Kills”) then
local Kills = Killer.leaderstats.Kills
Kills.Value = Kills.Value + 1
end
return
end
end
end)
end
end)
end)

1 Like

hahaha “you will create more games”… u are funny :wink: this one gives me grey hair but my kids really want to do it hahahaha
but i will ofc save it in case of :slight_smile:

1 Like

haha the death and kills only work sometimes…

1 Like

I guess its this part that is the kills and death… but i have no idea how to re-wright it to fit the script…
When i look at yt i just find it in leaderstat text… so i dont really know how to use it…
plr.CharacterAdded:connect(function(char)
local humanoid
repeat
humanoid = char:FindFirstChild(“Humanoid”)
wait()
until humanoid
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
local tag = humanoid:FindFirstChild(“creator”)
if tag then
local killer = tag.Value
if killer then
killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
end
end
end)
end)
end)

2 Likes

does anyone know how to add the kill and death into the script?

add this in a different topic. thats the rules i think

i wouldnt really say that its a different topic since it was a code that was in the original script… but i already did a new topic about it :slight_smile: and ty for your help :slight_smile:

im about to make a tutorial with it… so you know what’s going to happen…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.