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

How do you make leaderstats that will save for the player?

local dss = game:GetService("DataStoreService")
local data = dss:GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(plr)
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local coins = Instance.new("IntValue", folder)
	coins.Name = "Coins"
	print("Sucess!")
	
	local pd 
	
	local success, errormessage = pcall(function()
	pd = data:GetAsync(plr.UserId.."-coins")
	end)
		if success then
			coins.Value = pd	
	else
		print("There was an error")
				warn(errormessage)
		end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
       local success, errormessage = pcall(function()
	data:SetAsync(plr.UserId.."-coins", plr.leaderstats.cash.Value)
	end)
		
		if success then
			print("Player's Data has been saved!")
	else
			print("Player's Data has not been saved!")
			warn(errormessage)
		end
end)

but it printed "Players data hasn't been saved!"

15 comments

0

Make sure you have studio access to API services like DataStore in your game settings.

0

How do i check that?

0

Im a beginner i just learned datastores by alvin Blox

0

home tab-game settings-options

0

Ohhhhhh Thankyou so much.

0

np, as a general rule whenever I start up a new project i immediately enable studio access to api services

0

It still says Player's Data has not been saved! 14:36:57.680 - cash is not a valid member of Folder

0

Hi, check this out - https://developer.roblox.com/en-us/articles/Saving-Player-Data ////// in the script in there change the money part into the name of your currency.

0

in line 29 you said plr.leaderstats.cash.Value instead of plr.leaderstats.coins.Value

0

I've read that multiple times before.

0

Ok it doesn't warn me of any error message but how do i test it to make sure it really works

0

You can just press play in Roblox Studio, change your Coins value on the server, and then hit 'Stop', it'll tell you if it saved by printing out your success message, and if you play it again your Coins should be the same. You can do it inside of Roblox Studio.

0

But what if i play it again and the value is 0?

0

Well, first of all make sure to change it on the server. And if it didn't work then it should print out your error message.

0

I still dont get it

2 answers

✓ Accepted answer

Just as a heads up, in the SetAsync (Line 29), you've set plr.leaderstats.cash.Value when it is not cash, you created 'Coins', make sure to change that otherwise you'll get an error.

This is super simple to do just type this and im assuming you know how to make a basic leaderboard.

script


local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CoinSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local leaderstats = Instance.new("Folder", plr)
 leaderstats.Name = "leaderstats"
 local Coin = Instance.new("IntValue", leaderstats)
 Coin.Name = "Coins"

 
 Coin.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, Coin.Value)
 
 
 Coin.Changed:connect(function()
  ds1:SetAsync(plr.UserId, Coin.Value)
 end)
 
 Coin.Changed:connect(function()
	print("success")
 end)
end)

Then Enable Studio Access to API Services: and turn it on. When you turn it on it should show a check mark! in the box or if you go to studio and open game setting and click On on API service.

1 comment

0

Thankyou

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