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

What is wrong with my code that is preventing my table from inserting results?

Asked by 4 years ago

Here is the code, it is not putting the (Pet).Name into the table PetsFolder, and saving and loading through DataStore? What am I doing wrong, or missing? Thanks in advance for anyone that can help fix the problem.

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PetDataStore = DataStoreService:GetDataStore("TestingPets1")

local dataLoaded = nil
local dataAttempts = 3
local playerPets = {}

local function get(plr)

    local pKey = "pPets_"..plr.UserId
    local serialized

    local count = 0
    local success, err

    repeat
        success, err = pcall(function()
            serialized = PetDataStore:GetAsync(pKey)
        end)

        count = count + 1

        wait(0.5)
    until count >= dataAttempts or success

    if not success then
        warn("Failed to read or load data. Error code: " .. tostring(err))
        return
    end

    if serialized then
        return serialized
    else 
        return {};
    end

end

local function Set(plr)
    if dataLoaded then
        local pKey = "pPets_"..plr.UserId
        local data = playerPets

        local success, err
        local count = 0

        for i, v in pairs(plr.Pets:GetChildren()) do
            table.insert(playerPets, v.Name)
        end

        repeat
            success, err = pcall(function()
                PetDataStore:SetAsync(pKey, data)
            end)

        count = count + 1

        wait(0.5)

        until count >= dataAttempts or success

        if success then
            print(unpack(playerPets))
        end

        if not success then
            warn("Failed to serialize data. Error code: " .. tostring(err))
            return
        end
    end
end

local function CreatePets(plr)
    local PetsFolder = Instance.new("Folder")
    PetsFolder.Name = "Pets"
    PetsFolder.Parent = plr

    dataLoaded = true
end

players.PlayerAdded:Connect(CreatePets)
players.PlayerRemoving:Connect(Set)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Problem

It is a Obvious Problem. You never told it to Load data from the DataStore.

Solution

  1. After creating the Folder you want to call get(plr)
  2. Then you want to loop trough the Result of get, each result than gets added into the pets folder

Also you may also want to game:BindToClose() cause if you are the last player. The server can shutdown before the Data gets saved

Final Script

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PetDataStore = DataStoreService:GetDataStore("TestingPets1")

local dataLoaded = nil
local dataAttempts = 3
local playerPets = {}

local function get(plr)

    local pKey = "pPets_"..plr.UserId
    local serialized

    local count = 0
    local success, err

    repeat
        success, err = pcall(function()
            serialized = PetDataStore:GetAsync(pKey)
        end)

        count = count + 1

        wait(0.5)
    until count >= dataAttempts or success

    if not success then
        warn("Failed to read or load data. Error code: " .. tostring(err))
        return
    end

    if serialized then
        return serialized
    else 
        return {};
    end

end

local function Set(plr)
    if dataLoaded then
        local pKey = "pPets_"..plr.UserId
        local data = playerPets

        local success, err
        local count = 0

        for i, v in pairs(plr.Pets:GetChildren()) do
            table.insert(playerPets, v.Name)
        end

        repeat
            success, err = pcall(function()
                PetDataStore:SetAsync(pKey, data)
            end)

        count = count + 1

        wait(0.5)

        until count >= dataAttempts or success

        if success then
            print(unpack(playerPets))
        end

        if not success then
            warn("Failed to serialize data. Error code: " .. tostring(err))
            return
        end
    end
end

local function CreatePets(plr)
     local PetsFolder = Instance.new("Folder")
     PetsFolder.Name = "Pets"
     PetsFolder.Parent = plr

    local Data = get(plr)

    for _, Pet in pairs(Data) do
        -- Do some stuff right here
    end   

     dataLoaded = true
end

players.PlayerAdded:Connect(CreatePets)
players.PlayerRemoving:Connect(Set)
0
What would I put in the -- Do some stuff right here section? NotPhxed 0 — 4y
0
you spawn your pets in. Luka_Gaming07 534 — 4y
Ad

Answer this question