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

Why won't this data script work even though there are no errors?

Asked by
Echtic 128
6 years ago

The problem is i'm still new at this and i don't know if it doesn't save or if it doesn't load or maybe both so i could really use some help with it. Here's the script:

--Variables--
local DataBaseStore = game:GetService("DataStoreService"):GetDataStore("AllTheStatsAreHere")
local Statsholder = script.Stats


game.Players.PlayerAdded:Connect(function(NewPlayer)

    local key = "Player-ID:" .. NewPlayer.UserId
    local GetSave = DataBaseStore:GetAsync(key)

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

    for i, Stats in pairs(Statsholder:GetChildren()) do
        local NewStats = Instance.new(Stats.ClassName)
        NewStats.Name = Stats.Name
        NewStats.Value = Stats.Value
        NewStats.Parent = DataBaseFolder
    end

    if GetSave then

        for i, Stats in pairs(script.Stats:GetChildren()) do

            DataBaseFolder[Stats.Name].Value = GetSave[i]
            print(i, Stats.Name, Stats.Value)

            end

    else -- Save Stats

        local StatsToSave = {}

            for i, Stats in pairs(DataBaseFolder:GetChildren()) do

                table.insert(StatsToSave, Stats.Value)
                print(i, Stats.Name, Stats.Value)

            end

            DataBaseStore:SetAsync(key, StatsToSave)


        end

        while wait(30) do  --Autosave

            local StatsToSave = {}

            for i, Stats in pairs(DataBaseFolder:GetChildren()) do

                table.insert(StatsToSave, Stats.Value)
                print(i, Stats.Name, Stats.Value)

            end

            DataBaseStore:SetAsync(key, StatsToSave)

        end

        game.Players.PlayerRemoving:Connect(function(OldPlayer)


            local key = "Player-ID:" .. OldPlayer.UserId

            local StatsToSave = {}

            for i, Stats in pairs(DataBaseFolder:GetChildren()) do

                table.insert(StatsToSave, Stats.Value)
                print(i, Stats.Name, Stats.Value)

            end

            DataBaseStore:SetAsync(key, StatsToSave)

        end)


end)
0
Have you tried it in-game? Datastores won't work offline. oftenz 367 — 6y
0
tried it both in studio with enabled api and in the actual game Echtic 128 — 6y
0
What error are you getting? It's easier to help if you gave us the error you're having. And where is "Stats"? Risa_Azumi 13 — 6y

Answer this question