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

Why don't my leaderstats or any stats save?

For a bit more detail, everything is enabled in this game. This effects both in and out of studio. I've tried the common form of saving leaderstats. This is the script I am using for my leaderstats:

local ds = game:GetService("DataStoreService"):GetDataStore("LeaderStore")

nc0 = "Z-Bucks"
nc1 = "Waves"

sc0 = 0
sc1 = 0

game.Players.PlayerAdded:Connect(function(player)
	wait(0.1)
	local savedLevel = ds:GetAsync(player.UserId)
	
	local coins = player.leaderstats["Z-Bucks"]
	
	local coins1 = player.leaderstats.Waves
	
	if savedLevel then
		coins.Value = savedLevel[1]
		coins1.Value = savedLevel[2]
	else
		coins.Value = sc0
		coins1.Value = sc1
		
		ds:SetAsync(player.UserId,{coins.Value, coins1.Value})
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stats = player:FindFirstChild("leaderstats")
	if not stats then return end
	local vals = {stats:FindFirstChild(nc0),stats:FindFirstChild(nc1)}
	if vals and vals[1] and vals[2] then
		ds:SetAsync(player.UserId,{vals[1].Value, vals[2].Value})
	end
end)

If you could provide a script that works that will be appreciated!

3 comments

0

You're not initializing the leaderstats folder in the first place

0

That and please you should use a pcall for Datastores and everyting Async because Errors are a lot more likely there

0

I already have a leaderstats. But how would I use pcall and async? I am not that good at coding.

1 answer

Never mind, I found a script that saves it. Here is the script if anyone needs it:

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

game.Players.PlayerAdded:Connect(function(p)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = p

local Cash = Instance.new("IntValue")
Cash.Name = "Z-Bucks"
Cash.Parent = leaderstats

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

wait(2)

 local key = "id_"..p.UserId
 local Cashs = p.leaderstats.Waves
 local Surviveds = p.leaderstats["Z-Bucks"]
 local Cooldowns = p.Cooldown
 local SoundIDs = p.IdOfSound
 local soundPlayss = p.soundPlays
 local FPSs = p.showFPS

local Saved = DataStore:GetAsync(key)
if Saved then
	Cash.Value = Saved[1]
	Survived.Value = Saved[2]
else
	local SaveNum = {Surviveds.Value, Cashs.Value, Cooldowns.Value, SoundIDs.Value, soundPlayss.Value, FPSs.Value}
	DataStore:GetAsync(key, SaveNum)
end

end)

game.Players.PlayerRemoving:Connect(function(p) DataStore:SetAsync("id_"..p.UserId, {p.leaderstats.Waves.Value, p.leaderstats["Z-Bucks"].Value}) end)

You can add more values in case you needed more to be saved.

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