Whats wrong with my DataStore Script

Hey so for my YouTube tutorial we have alot of stuff we need to save for our SPTS type game. So I wanted it to make it easier for people and so I don’t get so many comments everytime we add something new to make it save, so I decided to use some loops to automatically save everything, when ever I print the result of the “GetAsync” it returns nil, when I print right before it should save everything prints fine so I was wondering whats wrong with the script

Script in ServerScriptService:

local DS = game:GetService("DataStoreService"):GetDataStore("testing4")
--FOLDERS_________
	-- leaderstats|
	-- Powers     |

game.Players.PlayerAdded:Connect(function(plr)
	local NumberForSaving = {}
	wait(1)
	local plrKey = "id_"..plr.UserId
	
	local success, res = pcall(function()
		return DS:GetAsync(plrKey)
	end)
	
	local index = 0
	
	if success then
		print("Success", res)
		if res then
			print("Res: ", res)
			for _, item in pairs(plr:GetChildren()) do
				if item:IsA("Folder") and item.Name == "Powers" or item.Name == "leaderstats" then
					for _, item2 in pairs(item:GetChildren()) do
						plr:FindFirstChild(item.Name):WaitForChild(item2.Name).Value = DS:GetAsync(item2.Name)
					end
				elseif not item.Name == "StarterGear" and not item.Name == "PlayerScripts" and not item.Name == "PlayerGui" then
					print(item, type(item))
					plr:WaitForChild(item.Name).Value = DS:GetAsync(item.Name)
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local savingItems = {}
	for _, item in pairs(plr:GetChildren()) do
		if item.Name ~= "StarterGear" and item.Name ~= "PlayerScripts" and item.Name ~= "PlayerGui" and item.Name ~= "Backpack" then
			if item:IsA("Folder") then
				for _, item2 in pairs(item:GetChildren()) do
					savingItems[item2.Name] = item2.Value
				end
			else
				savingItems[item.Name] = item.Value
			end
		end
	end
	
	print("Saving Items: ", savingItems)
	DS:SetAsync("id_"..plr.UserId, savingItems)
end)
1 Like

How to make a leaderstats saving script | Roblox Scripting - YouTube i recommend u to watch this

Umm you know that’s my video right. lol