Here is my server script in workspace:

local ds = game:GetService("DataStoreService")
local played = ds:GetDataStore("PLAY3D")
local invs = ds:GetDataStore("Inventory")
local function load(plr)
	local f = Instance.new("Folder",game.ServerStorage)
	f.Name = tostring(plr.Name)
    local playedbefore = Instance.new("BoolValue",plr)
	playedbefore.Name = "PlayedBefore"
	playedbefore.Value = played:GetAsync(plr.UserId,playedbefore.Value) or false
	wait()
	if playedbefore.Value == true then
		local inv = {}
		for i = 500000,0,-1 do
			local s, e = pcall(function()
				invs:GetAsync(plr.UserId,inv)
			end)
			if e then
				warn(e)
			else
				print("Loaded Data!")
				break
			end
		end
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local cl = game.ReplicatedStorage:WaitForChild(v):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(v):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	else
		local inv = {"Sword"}
		wait()
		local num = table.getn(inv)
		if num == 1 then
			for i, v in pairs(inv) do
				local cl = game.ReplicatedStorage:WaitForChild(v):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(v):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	end
end

local function save(plr)
    local inv = {}
	for i, v in pairs(game.ServerStorage:WaitForChild(plr.Name)) do
		table.insert(inv,#inv,v.Value)
		v:Destroy()
	end
	wait()
	for i = 50000,0,-1 do
		local s, e = pcall(function()
			invs:SetAsync(plr.UserId,inv)
		end)
		if e then
			warn(e)
		else
			break
		end
	end
	if plr:WaitForChild("PlayedBefore").Value == false then
		for i = 50000,0,-1 do
			local s, e = pcall(function()
				played:SetAsync(plr.UserId,true)
			end)
			if e then
				warn(e)
			else
				break
			end
		end
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
   load(plr)
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
    save(plr)
end)
game:BindToClose(function()
    for _, client in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(save)(client)
		wait(100)
    end
end)

Here is my server script in StarterPlayerScripts:

repeat warn("Parent is not a player."); wait() until script.Parent:IsA("Player")
local plr = script.Parent
local area = plr:WaitForChild("StarterGear")
if #area:GetChildren() > 0 then
	for i, v in pairs(area:GetChildren()) do
		local f = game.ServerStorage:WaitForChild(plr.Name)
		local ins = Instance.new("StringValue",f)
		ins.Name = "Item"
		ins.Value = v.Name
	end
end
area.ChildAdded:Connect(function(obj)
	local f = game.ServerStorage:WaitForChild(plr.Name)
	local ins = Instance.new("StringValue",f)
	ins.Name = "Item"
	ins.Value = obj.Name
end)

Could someone help me out here?

4 comments

0

What's the error in output?

0

Nothing.

0

I would definitely recommend using UpdateAsync over SetAsync and you shouldn't be constantly trying to SetAsync if it fails, you could overwrite their data when they join a new server. GetAsync only has one parameter. You should merge all your datastores into one table so you don't waste as many datastore requests. Did you try printing to see if the SetAsync line even get's called?

0

In your BindToClose function you are waiting 100 seconds and saving it to each player. The game will automatically shut down after 30 seconds so you shouldn't be yielding there.