I want to make a saving system for my game, that is, with a script I have to press a button so that when I exit the game everything is saved and so when I re-enter it remains where I was from where I am going to the things I did.
Thank you.
Given the way you framed this question, I’m going to assume that you are aware of DataStores. Just in case you’re not, there’s numerous helpful resources about them out there, and they’re relatively easy to use.
With that said, you could use DataStores to save the player’s location, stats, and anything else when the button is pressed, and then load it when the player rejoins.
Edit: You will need to make use of RemoteEvents to trigger the saving from a ScreenGui button if that is your desire.
Read this article, it may make it clear.
Your solution to this is to use DataStores
Datastores have a datastore name, key, and value. I will talk about this in a second
This is how to access a datastore (Server script)
local DataStoreService = game:GetService("DataStoreService") -- Get the data store service
local DataStore = DataStoreService:GetDataStore("PlayerCash5632") -- Name of the datastore
Make sure that the datastore name is unique and won’t write on other people’s stores
The data store I am going to show you is a data store to save money
Saving and getting data
A data store has function such as DataStore:GetAsync
and DataStore:SetAsync
which can be used to load and store things in the datastore.
Datastores are almost like varribles but are stored on every single server in your game
To Access data
function GetSavedData(key)
local savedData = nil
pcall(function() -- Always put this in a pcall so if it breaks for some reason, it will not break the script
savedData = DataStore:GetAsync(key)
end
return savedData
end
A data store has a key and a value.
A key is usually the user that is trying to save the data.
So for example this is what a datastore looks like
“Bob” = 34
“Laura” = 342
“Jack” = 30000
“PersonThree” = 34
(Not real usernames)
Saving data is mostly like getting data but its settings values instead of returning
function SetSavedData(key,value)
pcall(function() -- Always put this in a pcall so if it breaks for some reason, it will not break the script
savedData = DataStore:SetAsync(key,value)
end
end
A completed data store code for saving money would be like this
Completed code (Saving money)
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Money34668478")
function SaveMoney(key,moneyvalue)
pcall(function()
ds:SetAsync(key,moneyvalue)
end
end
function GetMoney(key)
pcall(function()
return ds:GetAsync(key)
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.MoneyValue.Value = GetMoney(plr.UserId)
end
game.Players.PlayerRemoving:Connect(function(plr)
SaveMoney(plr.UserId,plr.MoneyValue.Value)
end
I made this quick there might be typos. Please tell me
You probably want to use GetAsync instead of SetAsync in the GetMoney function.
Yes! I just fixed it. Thank you for pointing that out