I’m new to datastores and I don’t know how to store a table using it, how can I make this work?
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("table")
local data
local player_id
game.Players.PlayerAdded:Connect(function(player)
local success, errormessage = pcall(function()
player_id = player.UserId
data = datastore:GetAsync(player_id)
end)
for name, value in pairs do
print(name)
print(value)
end
game.Players.PlayerRemoving:Connect(function()
local tableee = {
["Water"] = "Important",
["Joe"] = "name",
["Hello"] = "hi"
}
datastore:SetAsync(player_id.UserId, tableee)
end)
end)
Error: invalid argument #1 to ‘pairs’ (table expected, got nil)
local data
local success, errormessage = pcall(function()
player_id = player.UserId
data = datastore:GetAsync(player_id)
end)
it should be something like
if data then
for name, value in pairs(data) do
print(name)
print(value)
end
end
if data does exist – i.e. if there is data saved, showing that the player is a player that have played this game before and has data, otherwise, there will be no data (as the player is a new player) and come up with an error
also I have noticed about the
you don’t need to do a separate variable there, just change where you have your playerID variable in the playerAdded function straightaway
Wait sorry, I forgot that you need a parentheses on a for loop. But the thing that I want to know is, how do I edit the things inside the table once I save it as a variable?
Wait sorry, I’m new to datastores and I honestly have no idea how to use it. Can you tell me how I can make a datastore table? And how I can edit the things inside? Sorry if this is a bit confusing, I’m just terrible at explaining things
You create Instance values – i.e. boolean values, string values, int values
You attempt to load in data from the data store you have created
if successful then you know it is an old player which you would change the value of these instance values to whatever their value is in the datastore
if unsuccesful then you know it is a new player which you will then set the default values for new joined players
You will then save the data for players when they leave the game
– an additional comment that I would have towards the way that you set up yours is to not have the player removing event inside the player added event as it doesn’t need to be that way, keep your code cleaner and have less indentations
–Just my OCD
–Let me just quickly type up an example datastore in my roblox studio
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("PlayerDatastore")
game.Players.PlayerAdded:Connect(function(player)
local money = Instance.new("IntValue")
money.Parent = player
money.Name = "Money"
local gems = Instance.new("IntValue")
gems.Parent = player
gems.Name = "Money"
local exp = Instance.new("IntValue")
exp.Parent = player
exp.Name = "Money"
local tableOfData
local success, errormessage = pcall(function()
tableOfData = datastore:GetAsync(player.UserId)
end)
if tableOfData then -- we know that it is a returning player whom already played this game before
for key, value in pairs(tableOfData) do
print(key)
print(value)
money.Value = tableOfData["Money"]
gems.Value = tableOfData["Gems"]
exp.Value = tableOfData["Exp"]
end
else -- we know that this player is new or their data got throttled (cached) or whatever you call it and have lost their data
--sets the default value for new players
money.Value = 0
gems.Value = 0
exp.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tableOfData = {
["Money"] = player.Money.Value,
["Gems"] = player.Gems.Value,
["Exp"] = player.Exp.Value
}
local success, errormessage = pcall(function()
datastore:SetAsync(player.UserId, tableOfData)
end)
end)
@UnskiIIedPro
To the extent of typing this long passage up, I would like you to fully understand datastore, or at least don’t feel intimidated by it, so please ask questions on what parts you are confused, as I would need to go to sleep soon
Unless you have everything happening on your game in just one script, you would have to have an instance value of some kind
you change it just by changing it’s value
IntValue.Value = IntValue.Value + 100
instance values are used, as they can be read even on local scripts, as well as for the reason that you just need to load values onto new instance values and save their value to your datastore in an orderly fashion
No, absolutely not, tables doesn’t have to be only integers, they can also integrate strings, boolean values, float (also int values) - decimals, or even table inside tables
local dataTable = {
["VIP"] = true,
["Money"] = 123,
["Gems"] = 100,
["LevelInfo"] = {
["Level"] = 100,
["Exp"] = 10000,
["ExpCap"] = 1000000 -- long way to level up lol
}
}
I think I get it to some extent now, I really appreciate your help. I’ll message you or respond to one of your replies if I have another question, thanks!