Inserting strings into a table inside of a datastore

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
image

if anyone can help me figure out how to add to the datastore table and save it then i would be very happy

Please try again formatting the code – that got extremely messed up.

i just fixed it. i dont post on here alot so i was trying to figure it out

No worries! :slight_smile: Much appreciated! I’ll take a look for you!

1 Like

i got help on discord to make the script if anyone needs what i did here it is

local SESSION_DATA = {}
local dataStoreService = game:GetService("DataStoreService")
local database = dataStoreService:GetDataStore("Data")
local players = game:GetService("Players")

local function PlayerAdded(plr, plrData)
	if SESSION_DATA[plr] == nil then
		SESSION_DATA[plr] = plrData
		print(SESSION_DATA[plr])
	end
end

local function PlayerRemoved(plr)
	if SESSION_DATA[plr] ~= nil then
		database:SetAsync(plr.UserId, SESSION_DATA[plr])
		SESSION_DATA[plr] = nil
		print(SESSION_DATA[plr])
	end
end

local function AddSkin(plr, skinName)
	if SESSION_DATA[plr] and SESSION_DATA[plr].Skins then
		table.insert(SESSION_DATA[plr].Skins, skinName)
		print("Skin added:", skinName)
	else
		print("Error: Skins table not found for player")
	end
end

players.PlayerAdded:Connect(function(player)
	local data = database:GetAsync(player.UserId)
	print("Player data:", data)
	PlayerAdded(player, data)
end)

players.PlayerRemoving:Connect(PlayerRemoved)

AddSkin(game.Players:WaitForChild("Bennydoes_stuff"), "Lava") 
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.