Your code isn’t handling API errors, it just ignores them. An API error can be something out of your control, for example internal server error or datastores being down. This is how you should handle errors:
local success, response = pcall(function()
return statsStore:GetAsync(Player.UserId)
end)
if success then
if response then
print("The user has data:", response) --proceed as usual
else
print("The user joined for the first time!") --create their default data
end
else
print("An error occured:", response)
--if this condition is true, you need to handle it, not just ignore it
--you can handle it in many ways, for example remake the API call until success or kick the player
--if you kick them you should ensure their blank data wont override their actual one
end
Also the data might be set to zero if the player leaves too fast after joining, before their data loads. If the values(such as equippedTrail and Stat) are parented before their values are assigned, the SetAsync call of PlayerRemoving will replace player data with the default data of those values(for example 0).
There’re more issues when dealing with player data, but I wont mention them to avoid overcomplicating the current topic. If you feel you may be handling data unsafely, its a good idea to use community datastore libraries that handle those issues in the background for you(such as ProfileService).