2

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!

Coder10101
  • 27
  • 5

1 Answers1

0

--remember to enable data stored in Roblox studio before testing. --There is more information about data stored and a guide for enabling it here:https://create.roblox.com/docs/cloud-services/datastores

--A data store acts similar to a dictionary so we'll have Player.UserId as key and an table of the upgrades as the value.

local DataStoreService = game:GetService("DataStoreService")
local upgradeDataStore = DataStoreService:GetDataStore("PlayerUpgrades")

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()
        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

game.Players.PlayerAdded:Connect(function(player)
    local data = LoadPlayerData(player.UserId)
    if data then
        sessionPlayerData[player.UserId] = data
    else
        sessionPlayerData[player.UserId] = {} -- an empty table
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    SavePlayerData(player.UserId, sessionPlayerData[player.UserId])
end)

Note that i haven't tested this and there might be some typos due to writing this on my phone. There might be some bugs because it was a while ago I used Roblox studio but hopefully the concept works.

trico
  • 81
  • 1
  • 4
  • 1
    So I could Do table.Insert(sessionPlayerData[player.UserId] ,"NameofUpgrade_Here") ..... In theory I could make it sessionPlayerData[player.UserId] = { " Upgrade1" , "Upgrade2" , "Upgrade3"} ??? Ty for the help – Coder10101 Jun 15 '23 at 23:24