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

How would I save leaderstats?

I'll make this short, so I am trying to make a currency save system, but it won't work. So If I change "lvl" to 5, next time when I join the game it'll say "0", that means that I have no currency, obviously, no output errors as far as I can see.

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashValueSaver")

players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency1 = Instance.new("IntValue")
	currency1.Name = "lvl"
	currency1.Parent = player.leaderstats
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)

	end)

Thanks!

1 comment

0

Why would you save the data when currency1's value is changed? It would result in a lot of " DataStore request was added to queue" warnings.

2 answers


CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Cash"
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 500
	end
	
	cash.Changed:connect(function(Val)
		CashStore:SetAsync("Points_"..player.Name, Val)
	end)
end
game.Players.PlayerAdded:connect(PlayerEntered)

I just wrote a script using the devfourm

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