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)
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)
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?
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"
}
}
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.
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.
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.
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?