Hello, I have made a DataStore system for loading and saving skins, but sometimes, when a new player joins, they get other players’ skins without buying them.
Here is the loading code:
local getSuccess, currentSkins = pcall(function()
return OwnedSkinsStore:GetAsync(playerUserId)
end)
if not currentSkins then
table.insert(currentSkins, "Default") --if the player has no skins, give them the default skin
end
if getSuccess4 then
print(table.unpack(currentSkins)) --print the skin list
end
for i, v in pairs(currentSkins) do
local skinValue = game.ReplicatedStorage.Skins:FindFirstChild(v):Clone()
skinValue.Parent = skinsInventory
--put loaded skins into a skinsInventory folder in ReplicatedStorage (each player has a unique folder, it makes one when a player joins)
end
and here’s the saving code:
for _, skin in pairs(game.ReplicatedStorage.OwnedSkins:FindFirstChild(playerUserId):GetChildren()) do
table.insert(playerSkins, skin.Name) -- add player skins to table
end
local setSuccess, errorMessage = pcall(function()
OwnedSkinsStore:SetAsync(playerUserId, playerSkins) -- save the data
end)
game.ReplicatedStorage.OwnedSkins:FindFirstChild(playerUserId):Destroy() -- delete the player's skinInventory folder
The saving code is connected to the player leaving, and the loading code is connected to the player joining.
I was thinking that it had something to do with the fact that I don’t clear the table before the data loads - but I wasn’t entirely sure on how to do that, either.
Any help is appreciated! (Also, please let me know if I’ve missed any key details or even if there’s any room for improvement in my code.)