I want to datastore a table but it needs to be string i need encode the table?
no it doesnt
you can datastore tables just fine
DataStore:SetAsync(key, data)
where
-
key
must be a string or integer -
data
can be any type, including tables.
So basically
local data = {
Coins = 200,
Rebirths = 0
}
DataStore:SetAsync(UserId, data)
I triwd the table it giving that error Unable to assign property Value. string expected, got nil
you have to check if data is correct
How are you trying to store it?
Can you send your code here? We aren’t going to write entire code just for you. We will try to re-write the code to fix the errors.
That error doesn’t seem related to a Datastore, seems like it is setting the value of a string value to nil?
DefaultData = {
Something = {}
}
Like that
Good, I was asking how you were trying to store it (the DataStore:SetAsync()
call).
use HttpService:JSONEncode()
and HttpService:JSONDecode()
example:
local dss = game:GetService("DataStoreService")
local hs = game:GetService("HttpService")
local ds = dss:GetDataStore("Data")
local randomThings = {
"wowowowow";
4;
"yoo";
}
--get data
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Data"
local data
if pcall(function()
data = hs:JSONDecode(ds:GetAsync("Player_"..player.UserId))
end)
for name, value in data do
local Type = type(value)
if type(value) == "boolean" then Type = "bool" end
local Value = Instance.new(string.format("%s%sValue", string.sub(Type, 1, 1):upper(), string.sub(Type, 2)))
Value.Name = name
Value.Value = value
Value.Parent = folder
end
print("No data")
end
addRandom(player)
folder.Parent = player
end)
--save data
game.Players.PlayerRemoving:Connect(function(player)
local data = hs:JSONEncode(player.Data:GetChildren())
pcall(function()
ds:SetAsync("Player_"..player.UserId, data)
end)
end)
--adding values
function addRandom(player)
local randomThing = randomThings[math.random(#randomThings)]
player.Data[tostring(randomThing)] = randomThing
end
Congratulations, you just converted a table into a table.
@King_GamingRobloxYT Here all the full code
local datastoreservice = game:GetService("DataStoreService")
local database = datastoreservice:GetDataStore("data15")
local SessionData = {}
local remote = game:GetService('ReplicatedStorage'):WaitForChild('SettingsFire')
local text = game:GetService('TextService')
task.wait()
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local values = Instance.new('Folder',player)
values.Name = 'Values'
local bank = Instance.new('IntValue',values)
bank.Name = 'Bank'
local Settings = Instance.new('Folder',player)
Settings.Name = 'Settings'
local BGM = Instance.new('StringValue',Settings)
BGM.Name = 'BGM'
local SFX = Instance.new('StringValue',Settings)
SFX.Name = 'SFX'
local CompanyName = Instance.new('StringValue',Settings)
CompanyName.Name = 'CompanyName'
local GRAPHICSMODE = Instance.new('StringValue',Settings)
GRAPHICSMODE.Name = 'GRAPHICSMODE'
local succses = nil
local PlayerData = nil
local attempt = 1
repeat
succses,PlayerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not succses then
warn(PlayerData)
task.wait(3)
end
until succses or attempt == 5
if succses then
print("Connected Data")
if not PlayerData then
print("Default Data Loaded")
PlayerData = {
["Cash"] = 999999999,
["Bank"] = 0,
["SFX"] = 'ON',
["BGM"] = 'ON',
["CompanyName"] = 'n/a',
FreeGiftsRedeemed = {},
FreeGiftsTime = 0
}
end
SessionData[player.UserId] = PlayerData
else
warn("Unable Get Data For", player.UserId)
player:Kick("Unable To Load Your Data, Try Rejoining")
end
cash.Value = SessionData[player.UserId].Cash
cash.Changed:Connect(function()
SessionData[player.UserId].Cash = cash.Value
end)
bank.Value = SessionData[player.UserId].Bank
bank.Changed:Connect(function()
SessionData[player.UserId].Bank = bank.Value
end)
if SFX.Value == nil then
SFX.Value = 'ON'
end
SFX.Value = SessionData[player.UserId].SFX
SFX.Changed:Connect(function()
SessionData[player.UserId].SFX = SFX.Value
end)
BGM.Value = SessionData[player.UserId].BGM
BGM.Changed:Connect(function()
SessionData[player.UserId].BGM = BGM.Value
end) remote.OnServerEvent:Connect(function(p,Event)
if Event == 'ON' then
p:WaitForChild('Settings'):WaitForChild('BGM').Value = 'ON'
elseif Event == 'OFF' then
p:WaitForChild('Settings'):WaitForChild('BGM').Value = 'OFF'
end
CompanyName.Value = SessionData[player.UserId].CompanyName
CompanyName.Changed:Connect(function()
SessionData[player.UserId].CompanyName = CompanyName.Value
end)
end)
end)
function playerLeaving(player)
if SessionData[player.UserId] then
local succses = nil
local errorMsg = nil
local attempt = 1
repeat
succses,errorMsg = pcall(function()
database:SetAsync(player.UserId, SessionData[player.UserId])
end)
attempt += 1
if not succses then
warn(errorMsg)
task.wait(3)
end
until succses or attempt == 5
if succses then
print("Data Saved for ", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
game.Players.PlayerRemoving:Connect(playerLeaving)
function serverShutdown()
print("Server Shutting Down")
for i,player in ipairs(game.Players:GetPlayers()) do
task.spawn(function()
playerLeaving(player)
end)
end
end
game:BindToClose(serverShutdown)
There’s a difference between tables and dictionaries. You can’t store dictionaries in datastore; with tables, you can. Use :JSONEncode()
to convert the dictionary into a string that can be stored in datastore. Then, when retrieving data, use :JSONDecode
to convert the string into a dictionary.
What you were thinking:
{
Cash = 9999999999,
BGM = "ON", --and so on
}
This is not a table, but a dictionary.
This is a table:
{
99999999999,
"On" --and so on
}
Here is the updated script. I’ve also optimised it a bit.
local datastoreservice = game:GetService("DataStoreService")
local database = datastoreservice:GetDataStore("data15")
local httpservice = game:GetService("HttpService")
local SessionData = {}
local remote = game:GetService('ReplicatedStorage'):WaitForChild('SettingsFire')
local text = game:GetService('TextService')
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
local values = Instance.new("Folder")
values.Name = "Values"
local bank = Instance.new("IntValue")
bank.Name = "Bank"
local Settings = Instance.new("Folder")
Settings.Name = "Settings"
local BGM = Instance.new("StringValue")
BGM.Name = "BGM"
local SFX = Instance.new("StringValue")
SFX.Name = "SFX"
local CompanyName = Instance.new("StringValue")
CompanyName.Name = "CompanyName"
local GRAPHICSMODE = Instance.new("StringValue")
GRAPHICSMODE.Name = "GRAPHICSMODE"
local success
local PlayerData
for attempt = 1, 5 do
success, PlayerData = pcall(function()
return database:GetAsync(player.UserId)
end)
if not success then
warn(PlayerData)
task.wait(3)
else
break
end
end
if success then
print("Connected Data")
if not PlayerData then
print("Default Data Loaded")
PlayerData = {
Cash = 999999999,
Bank = 0,
SFX = "ON",
BGM = "ON",
CompanyName = "n/a",
FreeGiftsRedeemed = {},
FreeGiftsTime = 0
}
PlayerData = httpservice:JSONEncode(PlayerData)
end
SessionData[player.UserId] = httpservice:JSONDecode(PlayerData)
else
warn("Unable Get Data For", player.UserId)
player:Kick("Unable To Load Your Data, Try Rejoining")
end
cash.Value = SessionData[player.UserId].Cash
cash.Changed:Connect(function(value)
SessionData[player.UserId].Cash = value
end)
bank.Value = SessionData[player.UserId].Bank
bank.Changed:Connect(function(value)
SessionData[player.UserId].Bank = value
end)
if not SFX.Value then
SFX.Value = "ON"
end
SFX.Value = SessionData[player.UserId].SFX
SFX.Changed:Connect(function(value)
SessionData[player.UserId].SFX = value
end)
BGM.Value = SessionData[player.UserId].BGM
BGM.Changed:Connect(function(value)
SessionData[player.UserId].BGM = BGM.value
end)
CompanyName.Value = SessionData[player.UserId].CompanyName
CompanyName.Changed:Connect(function(value)
SessionData[player.UserId].CompanyName = value
end)
remote.OnServerEvent:Connect(function(p, Event)
p:WaitForChild("Settings"):WaitForChild("BGM").Value = Event
end)
cash.Parent = leaderstats
bank.Parent = values
BGM.Parent = Settings
SFX.Parent = Settings
CompanyName.Parent = Settings
GRAPHICSMODE.Parent = Settings
leaderstats.Parent = player
values.Parent = player
Settings.Parent = player
end)
function playerLeaving(player)
if SessionData[player.UserId] then
local success
local errorMsg
for attempt = 1, 5 do
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, httpservice:JSONEncode(SessionData[player.UserId]))
end)
if not success then
warn(errorMsg)
task.wait(3)
else
break
end
end
if success then
print("Data Saved for ", player.Name)
else
warn("Unable to save for", player.Name)
end
SessionData[player.UserId] = nil
end
end
game.Players.PlayerRemoving:Connect(playerLeaving)
function serverShutdown()
print("Server Shutting Down")
for _, player in game.Players:GetPlayers() do
task.spawn(playerLeaving, player)
end
task.wait(5)
end
game:BindToClose(serverShutdown)
I did not. I converted a dictionary into a table.
Dictionaries cannot be stored in datastore.
Did you know? A dictionary is a table!
They are essentially the same, but no.
Dictionaries hold key-value pairs and tables just store values.
An array is a table, a dictionary is also a table.
List me the source where it says that you cannot store dictionaries.
This is getting off topic now, let’s just stop.
Please just admit that you’re wrong because your posts will effectively mislead the OP.
A dictionary is a table with key-value pairs, an array is a table with index-value pairs.
In a DataStore, you can store almost any type of data - string, number, boolean, tables (including arrays and dictionaries)…
You do NOT need to encode it into JSON. Please remove your posts as they’re misleading.