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

Why does my leaderstats script in serverscriptservice not save anything?

Hello. I am trying to make a game where you just sit and get lazyness and stuff. but my leaderstats wont work. can someone tell me why?

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("Lazyness")
local ds2 = ds:GetDataStore("LPS")

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

	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"

	local Lazyness = Instance.new("IntValue", folder)
	Lazyness.Name = "Lazyness"

	local LPS = Instance.new("IntValue", folder)
	LPS.Name = "LPS"

	Lazyness.Value = ds1:GetAsync(player.UserId) or 0
	ds1:GetAsync(player.UserId, Lazyness.Value)

	LPS.Value = ds2:GetAsync(player.UserId) or 1
	ds2:GetAsync(player.UserId, LPS.Value)
	
end)

game.Players.PlayerRemoving:Connect(function(player)

	local Lazyness = player.leaderstats.Lazyness
	local LPS = player.leaderstats.LPS

	ds1:GetAsync(player.UserId, Lazyness.Value)

	ds2:GetAsync(player.UserId, LPS.Value)
end)

1 answer

✓ Accepted answer

you never setted the value in synchronization. all you did was GetASync and didnt finish it with SetASync.

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("Lazyness")
local ds2 = ds:GetDataStore("LPS")

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

	local Lazyness = Instance.new("IntValue", folder)
	Lazyness.Name = "Lazyness"

	local LPS = Instance.new("IntValue", folder)
	LPS.Name = "LPS"
	
	local userid = "Player_"..player.UserId
	
	local LPSData
	local LazynessData
	local Success, Error = pcall(function()
		LazynessData = ds1:GetAsync(userid)
		LPSData = ds2:GetAsync(userid)
	end)
	
	if Success then
		Lazyness.Value = LazynessData
		LPS.Value = LPSData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local userid = "Player_"..player.UserId
	
	local Lazyness = player.leaderstats.Lazyness.Value
	local LPS = player.leaderstats.LPS.Value
	
	local Success, ErrorMessage = pcall(function()
		ds1:SetAsync(userid, Lazyness)
		ds2:SetAsync(userid, LPS)
	end)
	
	if Success then
		print("Successfully saved data for: "..player.Name)
	else
		warn("Could not save data for: "..player.Name)
		wait(2)
		error(ErrorMessage)
	end
end)

You can't just get the players name in synchronization and not set it after the player got removed.

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