Data won't save, API Services are enabled

The Data just doesn’t save, I have tried anything that I can think of however I can’t figure it out. Here is my current script:

local itemsFolder = game:GetService('ServerStorage'):WaitForChild('Swords')
local players = game:GetService('Players')
local dataStoreService = game:GetService('DataStoreService')
local mainStore = dataStoreService:GetDataStore('Main')

local function playerAdded(plr)
	repeat
		task.wait()
	until plr.Character
	task.wait(3)
	local success,inventory = pcall(function()
		return mainStore:GetAsync(plr.UserId)
	end)
	if success then
		for i,name in pairs(inventory) do
			local item = itemsFolder:FindFirstChild(name)
			if item then
				local clone = item:Clone()
				clone.Parent = plr.Swords
			end
		end
	end
end

local function playerLeft(plr)
	local backpack = plr.Swords
	local char = plr.Character
	local itemsTab = {}
	for i, object in pairs(plr.Swords:GetChildren()) do
		if object and object:IsA('Tool') then
			table.insert(itemsTab,object.Name)
		end
	end
	local success,err = pcall(function()
		return mainStore:SetAsync(plr.UserId,itemsTab)
	end)
	if not success then
		print(err)
	end
end
players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerLeft)

I recieve a single error everytime, whilst I am aware of what the problem is, I have 0 clue on how to fix it, I will try publishing to see if it is a studio issue but until then, I request that if you know what the problem is, you try and assist.

Idk what was up with my studio but it works in game.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.