In my game when people buy their upgrades, I want the name of the upgrade to add to be added to a list that so when buying an upgrade It can be referenced and Not buy the same upgrade again. I have figured out how to do this locally but once they leave the game, the list gets wiped. How would I send the list to the server and save that list to datastore so that it remembers their bought upgrades even after leaving the game? Any help is appreciated. Thanks!
(All I want to save is the Names of the Upgrades to the list, or is it array? Idk) Here is what I got so far...
local DataStoreService = game:GetService("DataStoreService")
local upgradeDataStore = DataStoreService:GetDataStore("ClickerUpgra")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local recieveUpgradeNameEvent = ReplicatedStorage.ShopEvents.SendUpgradeName
local sessionPlayerData = {} --this is the dictionary you add all of the data to
local function SavePlayerData(userId,data)
--function used to save player data
local success, errorMessage = pcall(function()
print("Success on saving that data ")
upgradeDataStore:SetAsync(userId, data)
end)
if not success then
print(errorMessage)
end
end
local function LoadPlayerData(userId)
--acceses player data from data store
local success, errorMessage = pcall(function()
return upgradeDataStore:GetAsync(userId)
end)
if not success then
print(errorMessage)
end
end
recieveUpgradeNameEvent.OnServerEvent:Connect(function(player, UpgradeName)
print("Recieved da event!!!")
print(player)
print(UpgradeName)
table.insert(sessionPlayerData[player.UserId], UpgradeName)
print(sessionPlayerData[player.UserId])
end)
game.Players.PlayerAdded:Connect(function(player)
local data = LoadPlayerData(player.UserId)
if data then
sessionPlayerData[player.UserId] = data
print("A player has data and is being set to them right now")
else
sessionPlayerData[player.UserId] = {} -- an empty table
print("Assigning default values")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Trying to save data my guy")
print(sessionPlayerData[player.UserId])
SavePlayerData(player.UserId, sessionPlayerData[player.UserId])
end)
Everything prints correctly and I can see the printed table of the session data, but it comes up blank and assigns the default value when I rejoin, please help!