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

My leaderboard wont autosave my cash and gems?

Here is my script

CashData = game:GetService("DataStoreService"):GetDataStore("CashData")
GemsData = game:GetService("DataStoreService"):GetDataStore("GemsData")

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('IntValue', plr)
    stats.Name = 'leaderstats'
    local cash = Instance.new('IntValue', stats)
    cash.Name = 'Cash'
    cash.Value = 0

    stats.Name = 'leaderstats'
    local cash = Instance.new('IntValue', stats)
    cash.Name = 'Gems'
    cash.Value = 0
end)


I tried to make it autosave but it wont work, i tried two diffrent ways but this is all ive got

1 answer

For first remember: You not need to get few datastores, cuz you can save multiple data in one datastore.

local DS = game:GetService("DataStoreService")
local StatsData = DS:GetDataStore("StatsData")

local prefix = "User_"

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

	local plrData = StatsData:GetAsync(prefix..player.UserId)
	
	if plrData ~= nil then --checking if player have data
		Gems.Value = plrData[1] --importing data
		Cash.Value = plrData[2]
	else -- saving data if player didnt have data
		StatsData:SetAsync(prefix..player.UserId, {
			player.leaderstats.Gems.Value; 
			player.leaderstats.Cash.Value;
		})
	end
end)

game.Players.PlayerRemoving:Connect(function(player) --saves when player leave
	StatsData:SetAsync(prefix..player.UserId, {
		player.leaderstats.Gems.Value;
		player.leaderstats.Cash.Value;
	})
end)

5 comments

0

you can use a numeric for loop for lines 21 and 22, and you dont need to check if :GetAsync() returns nil or not

0

also he can put the values inside separate datastores if he wants

0

I think multiple DataStores saving at once counts as more “saves”, and gets you needlessly closer to the save cap, right?

0

Yea.

0

@ EXpodo1234ALT separate datastores - bad idea, cuz why if you can put many data in one datastore?

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