Why does my trail saving system not working?

Greetings,

Can someone please check why it doesn’t save the trails? Thanks!

local RS = game:GetService("ReplicatedStorage")
local DSS = game:GetService("DataStoreService")
local TrailTable = DSS:GetDataStore("TrailTable")

game.Players.PlayerAdded:Connect(function(player)
	local trailfolder = Instance.new("Folder",player)
	trailfolder.Name = "Trails"
	
	local data = TrailTable:GetAsync(player.UserId.."-Trails")
	
	if data then
		for i, trail in pairs(data) do
			local petFolder = Instance.new("Folder")
			petFolder.Name = trail[1]
			petFolder.Parent = player:WaitForChild("Trails")
			
			local petName = Instance.new("StringValue")
			petName.Name = "Name"
			petName.Value = trail[1]
			petName.Parent = petFolder
			
			local petLevel = Instance.new("NumberValue")
			petLevel.Name = "Level"
			petLevel.Value = trail[2]
			petLevel.Parent = petFolder
			
			local petXP = Instance.new("NumberValue")
			petXP.Name = "XP"
			petXP.Value = trail[3]
			petXP.Parent = petFolder
			
			local petMultiplier = Instance.new("NumberValue")
			petMultiplier.Name = "Multiplier"
			petMultiplier.Value = trail[4]
			petMultiplier.Parent = petFolder
		end
	else
		print("No trails detected!")
	end
end)


function saveTrails(player)
	if player:FindFirstChild("Trails") then

		local trailinventory = {}

		for i, trail in pairs(player.Trails:GetChildren()) do
			table.insert(trailinventory,{trail.Name.Value, trail.Level.Value, trail.XP.Value, trail.Multiplier.Value})
		end

		local succes,errorMessage = pcall(function()
			TrailTable:SetAsync(player.UserId.."-Trails",trailinventory)
		end)

		if succes then
			print("Trails saved!")
		else
			print("Error: "..errorMessage)
		end
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	saveTrails(player)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		saveTrails(player)
	end
end)

I made this script with a tutorial from AlvinBlox and a script from a different user.

In the future, please provide more information and context when asking for help with your code. It’s difficult to troubleshoot issues without knowing if the code is producing any errors or output. Can you please provide me any error messages or output that you are seeing?

One problem might be that API Services are disabled in your game settings. To check, do the following:

  1. Open the Game Settings menu by clicking on the Settings button in the Home tab of the menu bar, and then clicking on the Game Settings option.

  2. In the left-hand navigation of the Game Settings menu, click on the Security tab.

  3. Check if the “Enable Studio Access to API Services” toggle is enabled. If it is not, click on it to enable it.

Run your game again to see if the issue has been resolved.

The api is enabled, also it says “Trails saved!” in the output in the studio so it should’ve worked. My theory is that it didn’t have time to save even though there is a BindToClose function. This is my first time trying to make a table script of this size so I don’t fully understand how they work.