How do I make a datastore that saves a leaderstat?

Documentation really doesn’t work when conveying the information of how to use something when the person doesn’t know enough about the base of what that utility is made of. Even if you read the documentation, you still need to have a decent understanding of modules to really understand what’s going on with your code. Luckily, there are tons of tutorials about modules all over the developer forums! :smiley:

Hope you have a great day!
Hope this helps! :wave:

This thread is already way too long but I’ll just put in my post.

For the most direct answer follow Post 12

Post 12

Only thing you should add is a pcall surrounding the web calls (Async)


If you want to use a Data Storage module, I would highly recommend ProfileService over any other modules like DataStore2. DataStore2’s single purpose has been deprecated since the release of DataStore v2 and DS2 is unsupported.

ProfileService is fully supported and gets constant updates, as well as having many vital features like session locking.

Try this script:


local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

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

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

	local Wins = Instance.new('NumberValue', leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0

	local value1Data = Wins

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
	end)

	if s then
	Wins.Value = value1Data --setting data if its success
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Wins.Value) --setting data
	end)
	if not s then game:GetService("TestService"):Error(e) 
	end
end)

I haven’t read the full thread yet. But does it change the value on the server or client? If it does on the client then it wont say since data stores are on the server. Make sure to check

Check out my script that i posted in the thread. It should save your wins.

That is the exact same script I was using… I think the other tutorial I watched just copied this one

That script didn’t work, sorry :confused:

Alright I figured it out. All I did was use a free model. I checked the model for viruses and read through the whole script. The script had some errors and typos so I changed a few things. This is what I have so far and it seems to be working:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local wins = Instance.new("StringValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats
	
	local data = nil
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.userId.."-wins") 
	end)
	
	if success and data ~= nil then 
		wins.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.userId.."-wins", player.leaderstats.Wins.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

I will study this script. I’ll give an update if something happens again.

2 Likes