How can I make a new value in my leaderstats Script?

Hello! So I have a leaderstats script and the only value currently is Tokens. I was wondering how can I add another Value?

local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Tokens = Instance.new("IntValue")
	Tokens.Name = "Tokens"
	Tokens.Parent = leaderstats
	local UserId = player.UserId
	local data
	local succes, errormessage = pcall(function()
		data = CoinStore:GetAsync(UserId)
	end)
	if succes then
		Tokens.Value = data
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local data = player.leaderstats.Tokens.Value
	CoinStore:SetAsync(UserId, data)
end)
2 Likes

Just use Instance.new(), I don’t get your question though.

I request you to read it without ignoring.

Like this-

local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Tokens = Instance.new("IntValue")
	Tokens.Name = "Tokens"
	Tokens.Parent = leaderstats
--example--
local example = Instance.new(classname, leaderstats) --Basically second parameter sets the parent.
example.Name = "Example"
example.Value = whatever
	local UserId = player.UserId
	local data
	local succes, errormessage = pcall(function()
		data = CoinStore:GetAsync(UserId)
	end)
	if succes then
		Tokens.Value = data
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local data2 = player.leaderstats.Tokens.Value --as you have already assigned a global variable "data", so it would set your old data, so change the name of the variable to "Data2". Learn scopes to understand what I mean.
	CoinStore:SetAsync(UserId, data2)
end)

You can even use a table to save multiple data, and there is one more trick besides table

local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Tokens = Instance.new("IntValue")
	Tokens.Name = "Tokens"
	Tokens.Parent = leaderstats
--example--
local example = Instance.new(classname, leaderstats) --Basically second parameter sets the parent.
example.Name = "Example"
example.Value = whatever
	local UserId = player.UserId
	local data
	local succes, errormessage = pcall(function()
		data = CoinStore:GetAsync(UserId)
	end)
	if succes then
		Tokens.Value = data["Token"] or 0
Example.Value = data["Exp"] or 0
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data2 = {
["Token"] = player.leaderstats.Tokens.Value;
["Example"] = player.leaderstats.Example.Value;
}
	local UserId = player.UserId
local success, err = pcall(function()
	CoinStore:SetAsync(UserId, data2)
end)
if success then
print("Data Successfully Saved")
else
warn(err)
end
end)
1 Like

Here’s How You can do it with your script

local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")

local Default_Data = { -- create new table and put any stat that you want to make here ( gems , coins .etc
    Tokens = 0;
    Gems  = 0;
}

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

	local Tokens = Instance.new("IntValue")
	Tokens.Name = "Tokens"
	Tokens.Parent = leaderstats

    local Gems  = Instance.new("IntValue")
	Gems .Name = "Gems"
	Gems .Parent = leaderstats


	local UserId = player.UserId
	local data

	local succes, errormessage = pcall(function()
		data = CoinStore:GetAsync(UserId)
	end)
	if succes then -- once the data loaded successfully we wanna check if the player have data or not
        -- if the player already have data we give him he's data from the data store else we give him the default one
        data = data or Default_Data
		Tokens.Value = data.Tokens 
        Gems .Value = data.Gems
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
      -- and here should save the data with same way we getting it in Player add event function
	local data = {
                  Tokens = player.leaderstats.Tokens.Value;
                  Gems  = player.leaderstats.Gems .Value;
        }
	CoinStore:SetAsync(UserId, data)
end)

and if want more understanding watch supi tutorial on how data store works

Ok sorry, I apologize, I read it wrong.

it’s ok but This table is read only Table it can’t be change

Can you show me your script though ?

Wait a min, I think my code don’t even have 31 lines of code…

Oops! wrong person, sorry! I will test out your script.

1 Like

Your script didn’t seem to save and had an error on line 31.

Oh, NP, do tell me if there’s any error.

Hey, if you have tested my script, did it errored, if yes, then please show your code, if it works, please mark me as a solution so that many people could get it.

Hey, I just updated my code, try it and tell me.

idk why i test it in studio and i work good but you can try this instead which fix the player add event may not trigger when test with 1 Player

local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")

local Default_Data = { -- create new table and put any stat that you want to make here ( gems , coins .etc
	Tokens = 0;
	Gems  = 0;
}



local function OnPlayerAdd(Player: Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Tokens = Instance.new("IntValue")
	Tokens.Name = "Tokens"
	Tokens.Parent = leaderstats

	local Gems  = Instance.new("IntValue")
	Gems .Name = "Gems"
	Gems .Parent = leaderstats


	local UserId = Player.UserId

	local succes, data = pcall(function()
		return CoinStore:GetAsync(UserId)
	end)

	if succes then -- once the data loaded successfully we wanna check if the player have data or not
		-- if the player already have data we give him he's data from the data store else we give him the default one
		data = data or Default_Data
		Tokens.Value = data.Tokens 
		Gems.Value   = data.Gems
	end
end


game.Players.PlayerAdded:Connect(OnPlayerAdd)

for _ , player in game.Players:GetPlayers() do
	task.spawn(OnPlayerAdd , player)
end


game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	-- and here should save the data with same way we getting it in Player add event function
	local data = {
		Tokens = player.leaderstats.Tokens.Value;
		Gems  = player.leaderstats.Gems.Value;
	}
	pcall( function() CoinStore:SetAsync(UserId, data) end)
end)

I don’t think using a function would solve this.

Hey, it’s a long time, I don’t think you used my script, please use it and tell if it errors.

Oh yes! Sorry, I had a lot of stuff going on in real life for a few hours. But uhm, I do have a question. Do you want me to try to use the top code or bottom code?

1 Like

Bottom one, top one is just showing how to make new values. Bottom one is for DataSaving as well.

There does seem to be a problem, there is an error and it only shows Tokens.

image

You need to write your classname, example - “IntValue”, “StringValue” etc.
Classname determines what kind of object it is.

Replace classname with the object you want. It was for your help actually.

Oh sorry! Woops I forgot about that.

1 Like