Help With Leaderstats

I have this script from a random free model i got a while back, its a saving leaderstat script and it works well, right now im using it for my currency called “tokens” but im trying to add another stat called “wins” for the amount of wins a player has, but what im trying has not been working.

I’m not very good at leaderstats, so here is the script, how would I add another stat to it?
(edit: this is the original script before i made the changes to make another stat)

local stat = "Tokens"
local startamount = 0

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
	local Cash = Instance.new("IntValue",leader)
leader.Name = "leaderstats"
	Cash.Name = stat
	Cash.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.Tokens.Value)
end)

change the cash.name, not the local it wont do anything

Didn’t change it cuz it doesnt do anything

wdym? if you just change the “local stat” it wont do anything, but if you change your cash.name = stat into cash.name = Tokens, it will change the leaderstat name to Tokens instead of stat

No, the stat variable stands for “Tokens”. Putting Cash.Name = stat will make the Cash stat named “tokens” not “stat”

You could copy this model and just change stat

And if that didn’t work you might be somehow overwriting the leaderboard that is working

It would make two folders named “leaderstats” and the datastore would freak out like my mother when she finds out I got a B on a test. (Unrelated comparison)

edit: thanks lol

2 Likes

Sorry about your mother lol, gl on your next exams

2 Likes

local stat = "Tokens"
local startamount = 0

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
	local Cash = Instance.new("IntValue",leader)
leader.Name = "leaderstats"
	Cash.Name = ”Cash”
	Cash.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
	end)

local Tokens = Instance.new("IntValue",leader)
leader.Name = "leaderstats"
	Tokens.Name = “Tokens”
	Tokens.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Tokens.Value)
Tokens.Changed:connect(function()
ds:SetAsync(player.UserId, Tokens.Value)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.Cash.Value, player.leaderstats.Tokens.Value)
end)

Repost*

You can make a new IntValue object for a new stat and add it to the player’s leaderstats folder to add it to this script. Then, using the DataStore, you can put up a similar logic to save and load the value of the new stat.

Here’s an illustration of how you might go about doing it:

local stat1 = "Tokens"
local stat2 = "Wins"
local startamount = 0

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:connect(function(player)
  local leader = Instance.new("Folder",player)
  leader.Name = "leaderstats"

  --// Create an IntValue for the first stat
  local stat1Value = Instance.new("IntValue",leader)
  stat1Value.Name = stat1
  stat1Value.Value = ds:GetAsync(player.UserId..stat1) or startamount
  stat1Value.Changed:connect(function()
    ds:SetAsync(player.UserId..stat1, stat1Value.Value)
  end)

  --// Create an IntValue for the second stat
  local stat2Value = Instance.new("IntValue",leader)
  stat2Value.Name = stat2
  stat2Value.Value = ds:GetAsync(player.UserId..stat2) or startamount
  stat2Value.Changed:connect(function()
    ds:SetAsync(player.UserId..stat2, stat2Value.Value)
  end)
end)

game.Players.PlayerRemoving:connect(function(player)
  --// Save the values of both stats when the player leaves
  ds:SetAsync(player.UserId..stat1, player.leaderstats.Tokens.Value)
  ds:SetAsync(player.UserId..stat2, player.leaderstats.Wins.Value)
end)

For the Tokens and Wins stats, this script will construct two IntValue objects, and it will use the DataStore to save and load the stats’ values. The script will create the leaderstats folder, two IntValue objects, and set the values of the stats to either the start amount or the saved values when a new player joins the game (if no saved value is found). The script will store the most recent values of both metrics in the DataStore when a player exits the game.

I hope this is useful. Please let me know if you have any questions or require any additional help.

Thanks for the help, Now I’ll know what to do next time thanks!

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