Save a leaderstats (Script)

I try any solution I know but nothing works with this code. Can you help me ?

     game.Players.PlayerAdded:Connect(function (plr)
	local stats = Instance.new("BoolValue",plr)
	stats.Name = "leaderstats"
	
	local Points = Instance.new("IntValue",stats)
	local RobuxDonate = Instance.new("IntValue",stats)
	RobuxDonate.Name = "R$ Donate"
	Points.Name = "Points"
	Points.Value =0
	
  end)

  script.Donate.OnServerInvoke = function(player, amount, recipient)
	player.leaderstats.Points.Value = player.leaderstats.Points.Value - amount
	local rec = game.Players:FindFirstChild(recipient)
	rec.leaderstats.Points.Value = rec.leaderstats.Points.Value + amount
  end
  • Is possibly
  • Is not possibly

0 voters

I forgot to precise. the help I want to know is the code I need to keep the “Points” and “RobuxDonation”

Please put this in the right category #development-support:scripting-support

1 Like

how to modify the category ? .

1 Like

Press the pencil at the top corner then press the category underneath that. Then select it as scripting support.

1 Like

For starters

There is no such thing as ‘Points’ in your game, you’ve called it [“R$ Donate”].

That line is creating an instance with no name, so is pretty much useless.

I just want to know how to save the script not know what is “useless”

When I say save the script these save “Point” and “Donation” which is R $ donation

1 Like

When you say ‘save a leaderstats’ do you mean saving it in to a datastore or just changing the value in the leaderstats to a new value.

1 Like

Save wit a datastore and automatic load

1 Like

The problem is that I absolutely have to save in his script

1 Like

Fixing


The script is lacking the usage of data stores if you’re looking for stats to be saved across sessions. There appears to be multiple problems at the end, especially dealing with exploits.

game:GetService("Players").PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder") -- wait why a BoolValue when folders are fine?
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = 0
	Points.Parent = stats

	local RobuxDonate = Instance.new("IntValue", stats)
	RobuxDonate.Name = "R$ Donate"
	RobuxDonate.Parent = stats

	script.Donate.OnServerInvoke = function(player, amount, recipient) -- this does not work if the script is inside ServerScriptService, remotes should be in ReplicatedStorage
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - amount -- make sure the value does not go negative lol

		local rec = game.Players:FindFirstChild(recipient) -- uh ok
		rec.leaderstats.Points.Value = rec.leaderstats.Points.Value + amount
	end
end)

Saving

Here’s a code sample of saving: The bad method.

local DataStore = game:GetService("DataStoreService"):GetDataStore("cooldatastore")

local saveTable = {}
for _, stat in next, stats:GetChildren() do
	saveTable[stat.Name] = stat.Value
end

DataStore:SetAsync(player.UserId, saveTable)
1 Like

Maybe have a watch of this video by @Alvin_Blox and see if that helps. It pretty much explains out how to save leaderstats values to a datastore.

https://youtu.be/DkYupSBUpes

Yes Alvin Blox is a good coder just the turorial create 2 leaderstats …

1 Like

This script saves the leaderstats?

1 Like

Not quite, updated the post.

Although beware of saving failures and you will need:

  • pcall
  • UpdateAsync over SetAsync
1 Like

I tested and it does not work (it’s a normal script mid-workpace) can you tell me why it does not work?

1 Like

In that case, you are not writing the code correctly:

local Players = game:GetService("Players")

local DataStore = game:GetService("DataStoreService"):GetDataStore("cooldatastore")
local session = {}

Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder") -- wait why a BoolValue when folders are fine?
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = 0
	Points.Parent = stats

	local RobuxDonate = Instance.new("IntValue", stats)
	RobuxDonate.Name = "R$ Donate"
	RobuxDonate.Parent = stats

	script.Donate.OnServerInvoke = function(player, amount, recipient) -- this does not work if the script is inside ServerScriptService, remotes should be in ReplicatedStorage
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - amount -- make sure the value does not go negative lol

		local rec = game.Players:FindFirstChild(recipient) -- uh ok
		rec.leaderstats.Points.Value = rec.leaderstats.Points.Value + amount
	end
	
	-- loading
	local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)
	
	if success then
		if data then
			session[player.UserId] = data -- or set the values of each stat here
		else
			-- default data
		end
	else
		-- error
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local stats = player:FindFirstChild("leaderstats")
	if not stats then return end -- what if it didn't exist first lol

	local saveTable = {}
	for _, stat in next, stats:GetChildren() do
		saveTable[stat.Name] = stat.Value
	end

	local success, errorMessage = pcall(DataStore.SetAsync, DataStore, player.UserId, saveTable)
	if not success then
		-- failed to save
	end
end
1 Like

wat is the "local session = ‘’ " ?

1 Like

i have you code and … image i d’ont have leaderstats

1 Like

Sometimes, I don’t favor keeping the stats in leaderstats. So I always keep a table somewhere around the script to be stored with the data to be saved.

As you can see, the previous script do create memory leak problems due to the fact that the table is filling up as players join. That’s why I have to fix the last block like this:

Players.PlayerRemoving:Connect(function(player)
	local stats = player:FindFirstChild("leaderstats")
	if not stats then return end -- what if it didn't exist first lol

	local saveTable = {}
	for _, stat in next, stats:GetChildren() do
		saveTable[stat.Name] = stat.Value
	end

	local success, errorMessage = pcall(DataStore.SetAsync, DataStore, player.UserId, saveTable)
	if not success then
		-- failed to save
	end

	session[player.UserId] = nil
end

Speaking of which, I’ll re-arrange the code for you in your PMs(although it is not advised to ask for free scripts lol)

1 Like