Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Need help on how to add another intvalue to my datastore?

Asked by 1 year ago
Edited 1 year ago

The datastore i am using is from a youtuber i copied because it is closest to how i want to save my items/info. Only problem is i am stuck on how to save and load something that has 3 values. The way it saves, it saves a folder then it saves intvalues under it but my items have three things i need to save. Item name, amount and image id. An int value can only do the name and either the amount or image id.

I made a workaround just using that but the further i got into my game and the inventory gui's, the more redundant my code is because i am having to always try and get the items image id from somewhere else. I have tried to fix this but i am not good with tables. what i want to be able to save is a folder named items, and inside that is a part(thats named) then under that is two intvalues.

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Player")

local loaded = {}

game.Players.PlayerAdded:Connect(function(player)
    local success, value = pcall(dataStore.GetAsync, dataStore, player.UserId)
    if success == false then player:Kick("DataStore failed to load, please try again.") return end
    local data = value or {}
    print ("loaded:", data)
    for i, folder in game.ServerStorage.PlayerData:GetChildren() do
        local subData = data[folder.Name] or {}
        local clone = folder:Clone()
        for i, child in clone:GetChildren() do
            child.Value = subData[child.Name] or child.Value
        end
        clone.Parent = player
    end
    loaded[player] = true
end)

game.Players.PlayerRemoving:Connect(function(player)
    if loaded[player] ==nil then return end
    local data = {}
    for i, folder in game.ServerStorage.PlayerData:GetChildren() do
        local subData = {}
        for i, child in player[folder.Name]:GetChildren() do
            subData[child.Name] = child.Value
            end
        data[folder.Name] = subData
        player[folder.Name]:Destroy()

    end
    local success, Value = pcall(dataStore.SetAsync, DataStore, player.UserId, data)
    print("Saved:", data)
    loaded[player] = nil
end)

game:BindToClose(function()
    while next(loaded) ~= nil do
        task.wait()
    end
end)

Answer this question