Im trying to insert a new skin into my “Skins” table that is supposed to have all of the marble skins you own inside of it
whenever i add to the table it doesnt save the changes into the datastore
I have tried chatGpt and youtube to try and fix this problem.
Here is the dataStore script and the script that is supposed to update the skins table
DataStore:
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local database = dataStoreService:GetDataStore("Data")
local sessionData = {}
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("assiging default data")
playerData = {
["Cash"] = 100,
["Skins"] = {"Plastic"}
}
end
sessionData[player.UserId] = playerData
print(playerData.Skins)
else
warn("Unable to get data for",player.UserId)
player:Kick("Unable to load your data. Try again later.")
end
cash.Value = sessionData[player.UserId].Cash
cash.Changed:Connect(function()
sessionData[player.UserId].Cash = cash.Value
end)
leaderstats.Parent = player
end
players.PlayerAdded:Connect(PlayerAdded)
function playerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId,sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for",player.Name)
else
warn("Unable to save for",player.Name)
end
end
end
players.PlayerRemoving:Connect(playerLeaving)
function serverShutdown()
if runService:IsStudio() then
return
end
print("Server shutting down")
for i, player in ipairs(players:GetPlayers()) do
task.spawn(function()
playerLeaving(player)
end)
end
end
game:BindToClose(serverShutdown)
Update Table:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local database = datastoreService:GetDataStore("Data")
local data = database:GetAsync(player.UserId)
local skins = data.Skins
table.insert(skins,"Lava")
data.Skins = skins
database:SetAsync(player.UserId,data)
print(data.Skins)
end)
end)
here is a screenshot of what the datastore prints when i rejoin the game then what it shows in the second script
if anyone can help me figure out how to add to the datastore table and save it then i would be very happy