Trail saving system not working

Greetings,

I am having trouble with my trail saving system loading in when the player joins, I don’t think there are issues with the saving as every time it outputs “Trails Saved!” (as it should). I get no errors. Code down below! Thank you.

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

--Events
local AddTrails = RS:WaitForChild("Trails"):WaitForChild("AddTrails")

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 trailFolder = Instance.new("Folder")
			trailFolder.Name = trail[1]
			trailFolder.Parent = player:WaitForChild("Trails")
			
			local trailName = Instance.new("StringValue")
			trailName.Name = "Name"
			trailName.Value = trail[1]
			trailName.Parent = trailFolder
			
			local trailLevel = Instance.new("NumberValue")
			trailLevel.Name = "Level"
			trailLevel.Value = trail[2]
			trailLevel.Parent = trailFolder
			
			local trailXP = Instance.new("NumberValue")
			trailXP.Name = "XP"
			trailXP.Value = trail[3]
			trailXP.Parent = trailFolder
			
			local trailMultiplier = Instance.new("NumberValue")
			trailMultiplier.Name = "Multiplier"
			trailMultiplier.Value = trail[4]
			trailMultiplier.Parent = trailFolder
			
			local trailRarity = Instance.new("StringValue")
			trailRarity.Name = "Rarity"
			trailRarity = trail[5]
			trailRarity.Parent = trailFolder
		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, trail.Rarity.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)
1 Like

add print statements in your playeradded code to see where it breaks

1 Like

It stops printing when I reach the:

if data then

Maybe it is from the saving? But it gives success in return.

EDIT: Just trying to get the post on the front page.

1 Like

AddTrails.OnServerInvoke = function(player)
saveTrails(player)
end

Uhh… sorry that event is for a different part of the script that has nothing to do with the datastore.

1 Like

Maybe remove that and change it to if data == nil then return end causing it to stop and not to continue with the script, if that does not work then there might not be any data. But also if data then should work as well, it might just be that your script is not saving data.

You can also try warn(data) to see what is there.
Something like:

game.Players.PlayerAdded:Connect(function(player)
	local trailfolder = Instance.new("Folder",player)
	trailfolder.Name = "Trails"

	local data = TrailTable:GetAsync(player.UserId.."-Trails")
	warn(data)
	if data == nil then warn("No trail data was found.") return end

	for i, trail in pairs(data) do
		print("Creating new trail.")
		local trailFolder = Instance.new("Folder")
		trailFolder.Name = trail[1]
		trailFolder.Parent = player:WaitForChild("Trails")

		local trailName = Instance.new("StringValue")
		trailName.Name = "Name"
		trailName.Value = trail[1]
		trailName.Parent = trailFolder

		local trailLevel = Instance.new("NumberValue")
		trailLevel.Name = "Level"
		trailLevel.Value = trail[2]
		trailLevel.Parent = trailFolder

		local trailXP = Instance.new("NumberValue")
		trailXP.Name = "XP"
		trailXP.Value = trail[3]
		trailXP.Parent = trailFolder

		local trailMultiplier = Instance.new("NumberValue")
		trailMultiplier.Name = "Multiplier"
		trailMultiplier.Value = trail[4]
		trailMultiplier.Parent = trailFolder

		local trailRarity = Instance.new("StringValue")
		trailRarity.Name = "Rarity"
		trailRarity = trail[5]
		trailRarity.Parent = trailFolder
	end
end)
2 Likes

It doesn’t even warn in the output, but it prints what I wrote after the GetAsync.

1 Like

If might just be that the table is empty because the GetAsync isn’t returning nil.

What does it print after you get the data?

2 Likes

Oh, nothing about the data, I used warn(data) as you told me, but it didn’t say anything, legit no sign of a warning anywhere about this. I just printed a random word to make sure the script didn’t stop randomly (I did this in multiple places).

Then it may be a saving issue, I really don’t know since this is my first script of this kind, could you please look into the save function?

1 Like

After adding a quick few lines to another script to create a temp trail, it seems your script is saving data, but your first arg in the table is nil.

You only made a small mistake for doing trail.Name.Value, as that would just call the trail name and not the value in the trail, i suggest changing the value name to something else or just saving the trail object name instead of whats inside the value.

{
                    [1] =  ▼  {
                       [1] = void, -- this should be "DevTest", as that is what i named it
                       [2] = 99999999999999,
                       [3] = 100000000000,
                       [4] = 100,
                       [5] = "5000"
                    }
                 }
2 Likes

Oh my god, I really missed that, since I am trying to make the full details of each trail available in a player’s inventory, I want to make it understandable, so I will change it to something else such as “Nickname”. I’ll be back once I am done.

Thank you a lot! Though the main issue is solved, it seems like the saving system is having trouble with saving everything as the “Rarity” value is completely missing out from the script. This is the last value present in the script, and it might have not enough time to save. I am using BindToClose so that shouldn’t have been meaning trouble.

Screenshot 2023-01-08 212857

Note that I have another datastore where I store the currencies. It is probably best to merge them, but doing so may lag the whole script out and maybe even break it.

Odd, because my temp trail has the rarity value there.
image

1 Like

I think I found the reason…

and yep i was right:
image

it did not save the others, it kept its old data

If you do it correctly, it won’t break.

1 Like

That won’t be the reason. It will still save the rarity value in the table either way.

I found another small mistake that probably is making the problem.
When creating the rarity value you forgot to set .Value instead you did: trailRarity = trail[5] which makes the script error.

Changing that to trailRarity.Value = trail[5] should fix everything and you will be good to go, hopefully no more errors or problems.

1 Like

In that screenshot, I introduced 3 more trails to my inventory, and deleted the old one that didn’t have the rarity so the script won’t crash.

Yep your edit is right, I think that’s the problem, I’m gonna fix now.

Ok this is gonna take a while I have to create a button so I can wipe my datastore through a script I can’t do it in the editor, is there a way to do it without an ingame script?

You can do game:GetService("DataStoreService"):GetDataStore("TrailTable"):RemoveAsync("youruserid-Trails") it will remove all your data

1 Like

Oh wait nvm it had the rarity stored, I thought I had to delete the item. Well, I will still reset it just in case.

Anyways Im getting a lot of those:

Is the game gonna wait and save them or at some point its just gonna abandon them?

Honestly, I’m not sure about those warnings it may save them you can check your data again to see if it saved them. It should’ve saved them anyway.

1 Like