Hey everyone I am currently trying to create a save slot system for my game. It is currently working but I want players to be able to choose which slot they would like to save data in (for example: If 6 slots are shown they can choose slot 4 and save data to this certain slot)
Currently I have this system:
Client
local SaveSlot = 2 --just an example
Remote:FireServer(SaveSlot)
Server:
local CurrentTabel = Handler:GetData(PlayersStudio, "BuildingData", Player) or {}
table.insert(CurrentTabel,SaveSlot,{TycoonName = FilteredText, BuildingNumber = Building})
Handler:SetData(PlayersStudio, "BuildingData", CurrentTabel, Player)
The trouble with this is that if the player chooses a slot higher than the one that has saved data in then the system saves the data but then if they go ahead and save data in 1 slot lower it removes the data of the last slot they saved data in.
I have just tired myself out from doing this for the past few hours. Please help
You shouldn’t be using table.insert as that will push all the other save slots back one. First, you should have a default empty table like so SaveData = {nil, nil, nil, nil}
and then when you want to save the data index into the array and set it equal to the new data SaveData[SaveSlot] = {TycoonName = FilteredText, BuildingNumber = Building}
That would be because you’re using table.insert which is intended to work with arrays. You’re looking for dictionaries here where to save, you assign a value to the indice defined by SaveSlot and retrieve data by fetching from the data table at the same indice.
You can also scope out your DataStores so that you have an easier time working with them. For example, you may want to append the number of the save slot to the DataStore in order to determine what slot you’re working with.
Thought it might be worth noting that we have table.create now which removes the need to initialise tables with blank values like this for predetermined size. You may want to remove the n key afterward as well.