Data not Loading When Character Joins Or Resets

Hello,
I have a script here that should load the player’s saved tools whenever the join or respawn. However, if you leave the game/reset, the scripts saves but doesn’t load. I have no idea why and I don’t know how to fix it.

Server script:

local ds = game:GetService("DataStoreService")
local tooldatastore = ds:GetDataStore("SaveTools")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
	local id = "Player_" .. player.UserId
	local data
	local success, err = pcall(function()
		
		data = tooldatastore:GetAsync(id)

	end)

	
		if data and success then
		for i, v in pairs(data) do
			
			local toolstoclone = game.ServerStorage.ToolsFolder:FindFirstChild(v)
		if toolstoclone then
				local clone = toolstoclone:Clone()
				clone.Parent = player:WaitForChild("Backpack")
				
		print('clone!')
				
	
			end
		end
	else
		print("The data was not retrieved correctly or the player had no data!")
end
	
	player.CharacterRemoving:Connect(function(character)
			character.Humanoid:UnequipTools()
			local tools = {}

			for i,v in pairs(player.Backpack:GetChildren()) do

				table.insert(tools, v.Name)

			end	
			local id = "Player_" .. player.UserId

			local success, err = pcall(function()
				tooldatastore:SetAsync(id, tools)
			end)

			if success then
				print("saved")
			else
				print(err)
			end
	end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	
	local tools = {}
	
	for i,v in pairs(player.Backpack:GetChildren()) do
		
			table.insert(tools, v.Name)
		
	end	
	local id = "Player_" .. player.UserId
	
	local success, err = pcall(function()
		tooldatastore:SetAsync(id, tools)
	end)
	
	
	if success then
		print("saved")
	else
		print(err)
	end
end)

No errors have appeared.

Template script for what you’re trying to achieve. You will need to modify it in order for it to fit your game.

local Game = game
local Players = Game:GetService("Players")
local ServerStorage = Game:GetService("ServerStorage")
local DataStoreService = Game:GetService("DataStoreService")
local ItemStore = DataStoreService:GetDataStore("ItemStore")

local function OnPlayerAdded(Player)
	local function OnCharacterAdded(Character)
		local function OnChildAdded(Child)
			if not (Child:IsA("BackpackItem")) then return end
			local StarterGear = Player:FindFirstChildOfClass("StarterGear") or Player:WaitForChild("StarterGear")
			if StarterGear:FindFirstChild(Child.Name) then return end
			Child:Clone().Parent = StarterGear
		end
		
		Character.ChildAdded:Connect(OnChildAdded)
		local Backpack = Player:FindFirstChildOfClass("Backpack") or Player:WaitForChild("Backpack")
		Backpack.ChildAdded:Connect(OnChildAdded)
	end
	
	Player.CharacterAdded:Connect(OnCharacterAdded)
	
	local Backpack = Player:FindFirstChildOfClass("Backpack") or Player:WaitForChild("Backpack")
	local StarterGear = Player:FindFirstChildOfClass("StarterGear") or Player:WaitForChild("StarterGear")

	local Success, Result = pcall(function() return ItemStore:GetAsync(Player.UserId) end)
	if not Success then warn(Result) return end
	if (not Result) or type(Result) ~= "table" then return end
	for _, ItemName in ipairs(Result) do
		local Item = ServerStorage:FindFirstChild(ItemName)
		if not Item then continue end
		Item:Clone().Parent = Backpack
		Item:Clone().Parent = StarterGear
	end
end

local function OnPlayerRemoving(Player)
	local StarterGear = Player:FindFirstChildOfClass("StarterGear")
	if not StarterGear then return end
	local Items = {}
	for _, Child in ipairs(StarterGear:GetChildren()) do
		table.insert(Items, Child.Name)
	end
	
	local Success, Result = pcall(function() return ItemStore:SetAsync(Player.UserId, Items) end)
	if not Success then warn(Result) end
end

local function OnGameClose()
	for _, Player in ipairs(Players:GetPlayers()) do
		OnPlayerRemoving(Player)
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
Game:BindToClose(OnGameClose)

I have gotten the error " GetAsync is not a valid member of DataStoreService “DataStoreService” on line 27, I have no idea how to fix this.

You need to call GetAsnync on the DataStore object, not the service.
For this example ItemStore:GetAsnyc(“Key”)

Ah ok, I did that. And it doesn’t seem to save/load. No errors or anything, just doesn’t load or save.

Alright,I managed to get it to work, thanks!

I forgot to change some references around after changing their names, I’ve edited the script above in case anyone else needs it.