For example, the players in my game have a "HouseType" value which is an integer value. If this is changed, I want to keep track of their previously owned houses so they can change back to an older house. Any suggestions on how to implement this?
Hey pickles,
local ds = game:GetService("DataStoreService"); local houses = ds:GetDataStore("Houses"); local plrs = game:GetService("Players"); plrs.PlayerAdded:Connect(function(plr) --Loading the data local key = plr.UserId; local success, house_types; local first_try = true; while not success do if not first_try then wait(7) -- waits 7 seconds because of DataStore limitations. end first_try = false; success, house_types = pcall( houses.GetAsync, houses, key ); end local current_house_type; if house_types then -- If there is any saved data. current_house_type = house_types[#house_types]; else current_house_type = "none"; -- The default house type if the player doesn't have any saved data. end -- And then you would set this current_house_type variable to some value in the game. end)
plrs.PlayerRemoving:Connect(function(plr) -- Saving data local key = plr.UserId; local current_house_type; -- You will get this from however you are keeping track of it in-game. It could be from a value inside the game or from a script somewhere... This will be the value to save. local success, response; local first_try = true; while not success do if not first_try then wait(7); end first_try = false; success, response = pcall( houses.SetAsync, houses, key, current_house_type or "none" -- ternary operator, basically the current_house_type or "none" will be saved. ) end end)
game:BindToClose()
which you can study more in detail here.~ Best regards,
~ Idealist Developer