How do we make a script save for every time for example: Just like in tycoon we get money we upgrade and when we leave the server and join back later. we want the tycoon to be saved. we don't want to restart again we want to have our data saved. But i don't really understand how to do so can someone explain how we can make a save script that saves things when we join a new server.
It saves a script to the stringvalue, so it saves script forever.
1 | local SaveScriptForever = Instance.new( "StringValue" ) |
2 | SaveScriptForever.Name = "SaveScriptForever" |
3 | SaveScriptForever.Value = "a" |
4 | SaveScriptForever.Parent = game.ServerStorage |
5 | scriptpathyouwanttosaveforever.Source = SaveScriptForever.Value |
You will have to use DataStoreService, it saves the data to the cloud.
Here is a little example of how to save data into the data store, i haven't tested it but what it does is that it takes all values from a specific folder (that you can select) and gets their names and then saves them into a table. Whenever a player joins, you should create a folder for him and whenever he buys a structure, create instance which will have the structure's name and put it into the folder (no matter what instance). Then when he joins back it should return the table with saved structure's strings and all you have to do then is just to check if player has the structure by it's name. Again i haven't tested it but if you will have any errors there you can ask.
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | local DataStore = DataStoreService:GetDataStore( "YOUR_DATA_STORE_NAME" ) |
05 |
06 | local function SaveData(Player) |
07 | if Player then |
08 |
09 | local OwnedStructuresFolder = Player:FindFirstChild( "OwnedStructuresFolder" ) -- Finds the structures folder |
10 |
11 | if OwnedStructuresFolder then |
12 | local OwnedStructuresToSave = { } -- Table in which we will save the strings |
13 |
14 | for _, OwnedStructure in pairs (OwnedStructuresFolder:GetChildren()) do -- For every single value in the folder do (_ is variable for index but we won't need it) |
15 |
Hey! By what you are saying, you need to learn about the DataStoreService. Also, you will need to know how to make a leaderboard, and if you dont, you should learn that. Here is an example script, put this in ServerScriptService
01 | -- Services |
02 | local Players = game.Players |
03 | local DataStoreService = game:GetService( "DataStoreService" ) |
04 |
05 | -- Data Store |
06 | local PlayerData = DataStoreService:GetDataStore( "PlayerData" ) |
07 |
08 | Players.PlayerAdded:Connect( function (Player) |
09 | -- Leaderboard (Dont change this) |
10 | local leaderstats = Instance.new( "Folder" ) |
11 | leaderstats.Name = "leaderstats" |
12 | leaderstats.Parent = Player |
13 |
14 | -- Cash |
15 | local Cash = Instance.new( "IntValue" ) |
To test this, just try and change the players cash in-game, and leave, then join back in. If you need help with this, put the following code in a script with its parent being a part, and touch the part
01 | local Part = script.Parent |
02 | local debounce = false |
03 |
04 | Part.Touched:Connect( function (hit) |
05 | local Player = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | if Player then |
07 | debounce = true |
08 | local Cash = Player.leaderstats.Cash |
09 | Cash.Value = Cash.Value+ 10 -- Change this to whatever |
10 | Part.TouchEnded:Wait() |
11 | debounce = false |
12 | end |
13 | end ) |
Hope this helps! Comment if something doesnt work, because i tested it and it should work.