i want to save tables, and i made a fairly working script but now i have this error, here’s the script
local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataKey1 = dataStoreService:GetDataStore("PlaceholderSongs3") -- used: 1, 2
Players.PlayerAdded:Connect(function(player)
local boolsFolder = Instance.new("Folder", player)
boolsFolder.Name = "Bools"
local songsFolder = Instance.new("Folder", boolsFolder)
songsFolder.Name = "Songs"
local playerkey = "bools_"..player.UserId.."_store"
dataKey1:GetAsync(playerkey)
local data = dataKey1:GetAsync(playerkey)
--local data2 = dataKey:GetAsync(equippedPlayerKey)
print(data)
if data ~= nil then
for i = 1,#data do
local V = Instance.new('BoolValue')
V.Name = data[i][1].Name
V.Value = data[i][1].Value
data[i][1] = V
V.Parent = songsFolder
for x = 2,#data[1] do
local V = Instance.new('BoolValue')
V.Value = data[i][x].Value
V.Name = data[i][x].Name
V.Parent = data[i][1]
end
end
end
end)
Players.PlayerRemoving:Connect(function(player)
local songsTable = {}
for i, v in pairs(player.Bools.Songs:GetChildren()) do
table.insert(songsTable, {v,v.InAlbum,v.placeholder})
print(songsTable)
end
local playerkey = "bools_"..player.UserId.."_store"
dataKey1:SetAsync(playerkey, songsTable)
end)
In your code you are trying to save the actual bool values to the datastore which isn’t allowed. You can only store basic lua data types in data stores.
Instead of saving “True” or “False”, try saving things that represent “True” or “False”
Like saving 0 and 1, where 0 represents true and 1 represents false
0 represents false, 1 represents true. True and False are basically the exact same as 1 and 0 respectively, just a friendlier way of displaying and typing it. (There is some under the hood conversion stuff too but that can be a tangent by someone else).
it’s weird that booleans are not allowed in a datastore either.
Instead of saving the entire instance you can just save the name of the instance and the value it should have. You can then recreate the instances from this data.
The problem stems from this part of your code:
for i, v in pairs(player.Bools.Songs:GetChildren()) do
table.insert(songsTable, {v,v.InAlbum,v.placeholder}) -- v is an instance (presumably v.InAlbum and v.placeholder are too)
print(songsTable)
end
local playerkey = "bools_"..player.UserId.."_store"
dataKey1:SetAsync(playerkey, songsTable) -- songsTable contains instances so cannot be saved to a datastore
Alright, tried that, seems to work, but now how would I recreate the instance on player join
I have this script here:
if data ~= nil then
for i = 1,#data do
local V = Instance.new('BoolValue')
V.Name = data[i][1].Name
--V.Value = data[i][1].Value
data[i][1] = V
V.Parent = songsFolder
for x = 2,#data[1] do
local V = Instance.new('BoolValue')
--V.Value = data[i][x].Value
V.Name = data[i][x].Name
V.Parent = data[i][1]
end
end
end
but when it happens it seems to give me this error here:
Unable to assign property Name. string expected, got nil.
still gives me the error. Here’s the script for now:
local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataKey1 = dataStoreService:GetDataStore("PlaceholderSongs9") -- used: 1, 2, 3, 4, 5, 6, 7 , 8
Players.PlayerAdded:Connect(function(player)
local boolsFolder = Instance.new("Folder", player)
boolsFolder.Name = "Bools"
local songsFolder = Instance.new("Folder", boolsFolder)
songsFolder.Name = "Songs"
local playerkey = "bools_"..player.UserId.."_store"
dataKey1:GetAsync(playerkey)
local data = dataKey1:GetAsync(playerkey)
--local data2 = dataKey:GetAsync(equippedPlayerKey)
print(data)
if data ~= nil then
for i = 1,#data do
local V = Instance.new('BoolValue')
V.Name = data[i][1].Name
V.Value = data[i][1].Value
data[i][1] = V
V.Parent = songsFolder
for x = 2,#data[1] do
local V = Instance.new('BoolValue')
V.Value = data[i][x].Value
V.Name = data[i][x].Name
V.Parent = data[i][1]
end
end
end
end)
Players.PlayerRemoving:Connect(function(player)
local songsTable = {}
for i, v in pairs(player.Bools.Songs:GetChildren()) do
table.insert(songsTable, {v.InAlbum,v.placeholder})
print(songsTable)
end
local playerkey = "bools_"..player.UserId.."_store"
dataKey1:SetAsync(playerkey, songsTable)
end)
It give’s me the error when i leave, not when i join, I also need the second parameter because it’s the name of the first value which is the parent of the other values