#Datastore help with scope

4 messages · Page 1 of 1 (latest)

lone marlin
#
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local Update_data = Remotes:WaitForChild("Update_Data")
local Players = game.Players

--local DataStoreOptions = Instance.new("DataStoreOptions")
--DataStoreOptions.AllScopes = true

local playerData = DataStoreService:GetDataStore("playerData", "skins")
local stats = DataStoreService:GetDataStore("playerData", "stats")

local hats = {}
local shirts = {}
local trousers = {}
local shoes = {}```
#
Players.PlayerAdded:Connect(function(player)
    print("Loading Data ", player.UserId)
    local playe_data = Instance.new("Folder")
    playe_data.Name = "playerData"
    playe_data.Parent = player
    
    local st = Instance.new("Folder")
    st.Name = "stats"
    st.Parent = playe_data
    
    local TC_Value = Instance.new("IntValue")
    TC_Value.Name = "TC"
    TC_Value.Parent = st

    local BP_Value = Instance.new("IntValue")
    BP_Value.Name = "BP"
    BP_Value.Parent = st

    local XP_Value = Instance.new("IntValue")
    XP_Value.Name = "XP"
    XP_Value.Parent = st

    local XPL_Value = Instance.new("IntValue")
    XPL_Value.Name = "XPL"
    XPL_Value.Parent = st
    
    local success, errorMessage = pcall(function()
        
        local TC = stats:GetAsync(tostring(player.UserId) .. "/stats/TC")
        local XP = stats:GetAsync(tostring(player.UserId) .. "/stats/XP")
        local BP = stats:GetAsync(tostring(player.UserId) .. "/stats/BP")
        local XPL = stats:GetAsync(tostring(player.UserId) .. "/stats/XPL")
        
        hats = playerData:GetAsync(tostring(player.UserId .. "/skins/hats"))
        shirts = playerData:GetAsync(tostring(player.UserId .. "/skins/shirts"))
        trousers = playerData:GetAsync(tostring(player.UserId .. "/skins/trousers"))
        shoes = playerData:GetAsync(tostring(player.UserId .. "/skins/shoes"))
    end)
    
    if success then
        TC_Value.Value = TC
        BP_Value.Value = BP
        XP_Value.Value = XP
        XPL_Value.Value = XPL
    else
        print("Data load error for ", player.UserId)
    end
    
    print("Data loaded ", player.UserId)
end)```
#
Players.PlayerRemoving:Connect(function(player)    
    local success, errorMessage = pcall(function()
        print("Saving Data", player, player.UserId)
        local st = player:WaitForChild("player_data"):WaitForChild("stats")
        
        local success, errorMessage = pcall(function()        
        stats:SetAsync(tostring(player.UserId) .. "/stats/TC", stats.TC.Value)
        stats:SetAsync(tostring(player.UserId) .. "/stats/BP", stats.BP.Value)
        stats:SetAsync(tostring(player.UserId) .. "/stats/XP", stats.XP.Value)
        stats:SetAsync(tostring(player.UserId) .. "/stats/XPL", stats.XPL.Value)
    
        playerData:SetAsync(tostring(player.UserId .. "/skins/hats"), hats)
        playerData:SetAsync(tostring(player.UserId .. "/skins/shirts"), shirts)
        playerData:SetAsync(tostring(player.UserId .. "/skins/trousers"), trousers)
        playerData:SetAsync(tostring(player.UserId .. "/skins/shoes"), shoes)
        end)
        
        if success then
            print("Data saved ", player.UserId)
        else
            print("Data saved error ", player.UserId)
        end
        

    end)
end)

Update_data.OnServerEvent:Connect(function(plr, item_type, item_id)
    print(item_type, item_id)
    if item_type == "shirts" then
        table.insert(shirts, item_id)
    end
end)```
#

Note: These are all in the one same script.