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

Having problems with saving a table using Datastore 2?

Asked by 2 years ago
Edited 2 years ago

I'm trying to use DataStore2 to save a folder. I've previously had trouble with saving folders with the normal Datastore, so there may be something wrong with what I'm doing. This is my code:

local DataStore2 = require (1936396537)

local e = {}

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

    local PetsDataStore = DataStore2("PetsDataStoree", player)

    local Folder = Instance.new("Folder")
    Folder.Parent = player
    Folder.Name = "Pets"

    local function petUpdate(value)
        e = PetsDataStore:Get(value)
        for _, pet in ipairs(e) do
            local PetValue = Instance.new("BoolValue")
            PetValue.Parent = Folder
            PetValue.Name = pet
        end
    end

    petUpdate(e)
    PetsDataStore:OnUpdate(petUpdate)

end)


    game.ReplicatedStorage.DataUpdate.OnServerEvent:Connect(function(player, value, DataType)
        local DataStore = DataStore2(DataType, player)

        if DataType == "PetsDataStoree" then
            local Pets = player.Pets
            local Pet = Instance.new("BoolValue")
            --Pet.Name = value
            Pet.Parent = Pets
        local ee = {}
        for _, v in ipairs(Pets:GetChildren()) do
            table.insert(ee, v.Name)
        end
        DataStore:Set(ee)
        end
    end)

Also a 2nd script inside of a local script, inside of a gui button


local PetsTable = {"Dog"} script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.DataUpdate:FireServer(PetsTable, "PetsDataStoree") end)

The purpose of this is: when the gui is clicked, it adds the pet into the pet data store, which is then supposed to save. The problem is, everytime I click the gui, it multiplies whatever is inside the pets folder by 2. When it saves, it then saves half of what was previously showed in the folder. I'm not sure why this is happening, it's just supposed to add 1 value into the table everytime I click the gui. Is there anything wrong with this script, or am I not using it right? Any help is appreciated, thanks!

0
Sorry about the "e" tables, I was changing those for testing purposes. Vesteria_Alt2 0 — 2y

Answer this question