I understand there are lots of different tutorials online but I all find them to either, not work for what I’m doing or just be a bit confusing. I have seperate folders in my Player. Let’s say I have a folder called PlayerValues. This stores all my Player Values, like my House Name and other stuff. I will also have a seperate folder called Inventory. This will have lots of bool values with furniture. Is Chair true or false etc. I was wondering if anyone knew the best and simplest way to do this.
Use Tables
Easy to edit and insert values, can be decoded and encoded using http service.
Example:
local DataMask = {
Items = {},
Coins = {}
Name = "bob"
}
You’d need to convert the folder heirarchy to a table by diving as deep as the deepest nested value and figuring out what the datatype of the value is, such that say,
PlayerValues > Furniture > Thing > IsChair = true
becomes
{PlayerValues = {Furniture = {Thing = {isChair = true}}}}
So I would do things like,Datamask { Item = {Chair = False, Table = true} Coins = {apple = 1, house = 2} }
If you have a lot of values, you will need to convert the objects into a single table(so-called serialization). This table can be stored into the data store, and then retrieved again later to be read and copied over to the next session.
To do this:
local data = {}
for _, object in pairs(PlayerValues:GetChildren()) do
data[object.Name] = object.Value
end
However, if it is nested, you will need to check if the object
is a folder. If it is, recursively do it.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("Noosty")
local House = {
["HouseName"] = "StarterHouse"
}
Players.PlayerAdded:Connect(function(Player)
local defaultValueHouse = "StarterHouse"
local DataFromStore = nil
local PlayerValues = Instance.new("Folder")
PlayerValues.Parent = Player
PlayerValues.Name = "PlayerValues"
for name, value in pairs(House) do
local new = Instance.new("StringValue")
new.Name = name
new.Value = value
new.Parent = PlayerValues
end
local s, e = pcall(function()
DataFromStore = MyDataStore:GetAsync("uid-" .. Player.UserId)
end)
if s then
print("Getting Data For: " .. Player.Name)
if DataFromStore then
for name, value in pairs(DataFromStore) do
Player.PlayerValues[name].Value = value
end
print("Data Loaded For: " .. Player.Name)
else
print("Created New Data For: " .. Player.Name)
end
else
warn("Error Getting Data For: " .. Player.Name)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local DataForStore = {}
for name, value in pairs(Player.PlayerValues:GetChildren()) do
DataForStore[value.Name] = value.Value
end
local success = pcall(MyDataStore.SetAsync, MyDataStore, "uid-" .. Player.UserId, DataForStore)
if success then
print("Successfully Saved Data For: " .. Player.Name)
end
end)
Does This work? Just Because I don’t want my HouseName and Inventory in the same place.
I have done this for one of the games I am currently in the making of. You want tables and within those tables, some more tables.
What I did was
local table1 = {}
for i,v in pairs(getchildren of something) do
table.insert(table1, v.Value)
end
local success, errormessage = pcall(function()
DS:SetAsync(player.UserId,table1)
end)
if errormessage then print(errormessage) else print("success!")
end
Something similar to that. Basically, I had a lot of values of data I had to record.
I think it works, I can’t remember when I tested it.
Hope this helps!
Didn’t mean to reply to you sorry!
using commas between the values but yea
I’m sorry but I’m still confused on how to impliment this in my script.
["HouseName"] = "StarterHouse",
["Inventory"] = {"Chair" = true, "bruh" = false}
}
doing that just errors out.
{
["HouseName"] = "StarterHouse",
["Inventory"] = {"Chair" = true, "bruh" = false}
}
local House = {
["HouseName"] = "StarterHouse",
["Inventory"] = {"Chair" = true, "bruh" = false}
}
Still errors out. Unless I do “Chair” == true
local House = {
HouseName = "StarterHouse",
Inventory = {Chair = true; bruh = false}
}
Staright up write it
dont use “”
Are you trying to set values to these objects?
Yeah, that way when a player loads in I can figure out whether a chair is in their inventory or not. Later I can impliment a placement system into this aswell. It’s all a bit complex for me.
Damn, now this snippet of code
for name, value in pairs(House) do
local new = Instance.new("StringValue")
new.Name = name
new.Value = value
new.Parent = PlayerValues
end
Doesnt work. String expected got Table. Because I was origionally creating string values.
some of this values are table
just check if its a table
for name, value in pairs(House) do
if type(value) == "table" then
local InventoryValues = Instance.new("BoolValue")
InventoryValues.Name = InventoryValues
InventoryValues.Value = value
InventoryValues.Parent = PlayerValues
else
local new = Instance.new("StringValue")
new.Name = name
new.Value = value
new.Parent = PlayerValues
end
end
I think I did this wrong. Now I got instance instead of table.
Basically, your script doesn’t know what you mean by “Chair” = true. I suppose one method to do this could be changing chair to Chair.true as a string. When you want to load it, you have to check the word after a . found.
then do a check for that too
Do check for each special value except strings,numbers and booleans