Trails DataStore not working

can anyone pls explain to me why the trails dont save and i get this warning when i leave the game?

warning: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = player_396958652

Players.PlayerAdded:Connect(function(player)
	
	local PlayerTrails = Instance.new("Folder",player)
	PlayerTrails.Name = "PlayerTrails"
	
	local TrailsFile = ServerStorage:WaitForChild("Trails")
	
	local data
	
	local success, Error = pcall(function()
		data = DataStore:GetAsync("player_"..player.UserId)
	end)
	
	if success then
		if data.trails then
			print("idk")
			for _, object in (data.trails) do
				print(object)
			end
			
			for _, trail in pairs(TrailsFile:GetChildren()) do
				for _, TrailName in pairs(data.trails) do
					if trail.Name == TrailName then
						local TrailClone = trail:Clone()
						TrailClone.Parent = PlayerTrails
						print(trail.Name)
					end
				end
			end
			print("loadded trails")
		end
	else
		print("there was an error while loading player trails")
		warn(Error)
	end
end

Players.PlayerRemoving:Connect(function(player)
	
	local PlayerTrails = player:WaitForChild("PlayerTrails") 
	if #PlayerTrails:GetChildren() == 0  then return end
	
	local data

	local success, Error = pcall(function()
		data = DataStore:GetAsync("player_"..player.UserId)
	end)
	
	data.Trails = data.Trails or {}
	
	if data.trails then
		for _, TrailName in pairs(data.trails) do
			for _,trail in pairs(PlayerTrails:GetChildren()) do
				
				if trail.Name == TrailName then break end
				
				table.insert(data.trails, trail.Name)
			end
		end
	end
	
	local success, Erorr = pcall(function()
		DataStore:SetAsync("player_"..player.UserId,data)
	end)

	if success then
		print("saved trails successfully")
	else
		print("there was an error while saving player trails")
		warn(Erorr)
	end
end)

Test the game/script in the client rather than studio.
Sometimes studio closes before the datastore can do it’s thing.

do you mean to test from the roblox page?

Yup

still doesnt work, can you think of any other reason?

Which line is the error coming from?

I’m not sure.
Can you try testing in studio with multiple players and see if it outputs a success or not when one of the players leave?

it comes from the pcall when saving data

The request added to queue warning doesn’t mean it actually failed if that’s what you’re seeing.
It’s just a throttle so you don’t overload the servers.

it doesnt save the trails though so there is a problem with the script

Did you try the multiplayer test in studio like I suggested? If not can you try it and see what it outputs?

i think i found the general problem, ill update soon

the problem is that data.trails = nil

why is that?

Try something like this and see if it helps:

local HS = game:GetService(“HttpService”)

—Saving
:SetAsync("player_"..player.UserId, HS:JSONEncode(data))

—Loading
data = HS:JSONDecode(data)

I’m on mobile so I can’t type as well or test it but I think it should work

Edit:
Also, remove the

if data.trails then

on the PlayerRemoving function

the warning was because i saved to the same datasore drom two different scripts
and here is the fixed script:

Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",leaderstats)
	Money.Name = "Money"
	Money.Value = 0
	
	local Wins = Instance.new("IntValue",leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0
	
	local PlayerTrails = Instance.new("Folder",player)
	PlayerTrails.Name = "PlayerTrails"

	local TrailsFile = ServerStorage:WaitForChild("Trails")
	
	-- load saved data
	local data

	local success, ErrorMsg = pcall(function()
		data = DataStore:GetAsync("player_"..player.UserId)
	end)

	if success and data then
		
		-- load leaderstats values
		Money.Value = data["Money"]
		Wins.Value = data["Wins"]
		
		-- load trails
		for _, trail in pairs(TrailsFile:GetChildren()) do
			for _, TrailName in pairs(data.trails) do
				if trail.Name == TrailName then
					local TrailClone = trail:Clone()
					TrailClone.Parent = PlayerTrails
					print(trail.Name)
				end
			end
		end
	else
		print("there was an error while loading player data")
		warn(ErrorMsg)
	end
	
	local PlayerGui = player:WaitForChild("PlayerGui")
	local ScrollingFrame = PlayerGui:WaitForChild("TrailsGui"):WaitForChild("TrailsFrame"):WaitForChild("ScrollingFrame")

	for _,trail in pairs(PlayerTrails:GetChildren()) do
		for _, TrailImage in pairs(ScrollingFrame:GetChildren()) do
			if TrailImage:IsA("ImageLabel") and TrailImage.Name == trail.Name then
				TrailImage.EquipButton.Text = "Equip"
				TrailImage.PriceText.Visible = false
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local Money = player.leaderstats.Money
	local Wins = player.leaderstats.Wins
	local PlayerTrails = player:WaitForChild("PlayerTrails")
	
	local data
	data = {Money = Money.Value, Wins = Wins.Value}
	data.trails = data.trails or {}
	
	if #PlayerTrails:GetChildren() > 0 then
		for i,trail in pairs(PlayerTrails:GetChildren()) do
			if table.find(data.trails, trail.Name) == nil then
				print(trail.Name)
				table.insert(data.trails, trail.Name)
			end
		end
	end
	
	local success, ErrorMsg = pcall(function()
		DataStore:SetAsync("player_"..player.UserId, data)
	end)
	
	if success == false then
		print("there was an error while saving players data")
		warn(ErrorMsg)
	end
end)

It still doesn’t work does it?
If it doesn’t can you try the :JSONEncode and :JSONDecode stuff i was talking about?

it does work though lol. i may try the jsone thing in the future

1 Like

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