How works the "save" system?

As the tittle say, I’m trying to make a “save” system for a leaderstats, I want that when a player have for exemple “5” as value for a leaderstats, when he rejoin, I want the player to have the same value (5). How would I do that?

You would use a datastore
extra charrrss

Hello there! In response to your question, I have a script for you from one of my games! Reply if it helps and a solution would be helpful.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Anynamehere") -- You can change Anynamehere to anything you want. BEWARE! - when you change it again, all data WILL be wiped.


game.Players.PlayerAdded:connect(function(player) -- Performes a function when the player joins the game
	local leader = Instance.new("Folder",player) -- Creates a folder called leaderstats. 
	leader.Name = "leaderstats"
	local Stat = Instance.new("IntValue",leader) -- Creates a value called Stat. You can change the stat part (only the part in SPEECH MARKS) to ANYTHING you want (your value)
	Stat.Name = "Stat"
	Stat.Value = ds:GetAsync(player.UserId) or 0 -- Looks into the datastore for any saved data or sets it to 0.
	
	Stat.Changed:connect(function()
		ds:SetAsync(player.UserId, Stat.Value) -- When Stat changes, it will set the value to the value of stat (e.g, if stat at start was 1 and it changed to 2, it would update that.)
	end)
	
end)

game.Players.PlayerRemoving:connect(function(player) -- Performes a function when the player leaves the game.
	ds:SetAsync(player.UserId, player.leaderstats.Stat.Value) -- Updates the value of stat for the final time when the player leaves the game.
end)

Hope this helps and check out any YouTube tutorials if you need things in extra detail. There are some great creators like AlvinBlox and TheDevKing which can help you out. Have a nice day/evening/night!

1 Like

Any bugs please tell me.
I’ll analyse the script instantly and report back.

1 Like

Thank you very much! I’m working on a big project with friends so I really need to know that :sweat_smile:

Instead of connect you need to do Connect as the lower case one is deprecated.

So that won’t work with connect?

It might still work, but it’s just a lot better to use the way I said.

1 Like

But why? What do it changes?

Anyways if the two ways works I’ll use the first one.

If im not wrong, deprecated means that it eventually will stop working, if you can, just use Connect

2 Likes

I’m sorry! I forgot to change that. Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.