Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

#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)

2 comments

0

What would I put in the -- Do some stuff right here section?

0

you spawn your pets in.

This discussion is preserved as it was. New answers, comments, and votes are closed.