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

Why isn't my cloned stat the same?

Asked by 4 years ago

I got this script. It should clone the stats and put them in a folder inside the player, but when I start the game and look at the values they're all 0. Please help me

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("main-ds")
local statsHolder = script.Stats

game.Players.PlayerAdded:Connect(function(newPlayer)
    local key = "player-id: " .. newPlayer.UserId
    local GetSave = dataStore:GetAsync(key)

    local DataBaseFolder = Instance.new("Folder", newPlayer)
    DataBaseFolder.Name = "PlayerStats"

    for i, v in pairs(statsHolder:GetChildren()) do
        local clone = v:Clone()
        clone.Name = v.Name
        clone.Value = v.Value
        clone.Parent = DataBaseFolder
    end

    local cloneHolder = statsHolder:GetChildren()
    cloneHolder.Parent = DataBaseFolder

    if GetSave then
        -- player has data
        print("Player has data")
        for _, Stats in pairs(statsHolder:GetChildren()) do
            DataBaseFolder[Stats.Name].Value = GetSave[_]
            print(_, Stats.Name, Stats.Value)
        end

    else -- save data

        local StatsToSave = {}

        for _, Stats in pairs(DataBaseFolder:GetChildren()) do
            table.insert(StatsToSave, Stats.Value)
            print(_, Stats.Name, Stats.Value)
        end
        dataStore:SetAsync(key, StatsToSave)

    end

    while wait(30) do -- auto save

        local StatsToSave = {}

        for _, Stats in pairs(DataBaseFolder:GetChildren()) do
            table.insert(StatsToSave, Stats.Value)
            print(_, Stats.Name, Stats.Value)
        end
        dataStore:SetAsync(key, StatsToSave)
    end

    game.Players.PlayerRemoving:Connect(function(oldPlayer) 
        local key = "player-id: " .. oldPlayer.UserId
        local StatsToSave = {}

        for _, Stats in pairs(DataBaseFolder:GetChildren()) do
            table.insert(StatsToSave, Stats.Value)
            print(_, Stats.Name, Stats.Value)
        end
        dataStore:SetAsync(key, StatsToSave)

    end)
end)

Answer this question