In every single topic and thread I’ve read on here, I haven’t seen an answer to this question. How do you load a non-default value from a DataStore?
From every tutorial I’ve seen, the script automatically creates the values for you and the DataStore simply gives those values the saved values from the data; but what if I need the DataStore to load values that the player added to their data during their last play session (example–buying something from a shop adds a BoolValue to the player, and that BoolValue is saved–but how would I add the BoolValue instance and its Value back to the player when they rejoin?)
Here’s the code I have so far:
local datastoreservice = game:GetService("DataStoreService")
local playerData = datastoreservice:GetDataStore("PlayerData")
local players = game:GetService("Players")
local function loadStats(player) --Load the player's stats on join
--Money
local leaderstats = script.leaderstats:Clone()
leaderstats.Parent = player
local money = leaderstats.Money
--Cosmetics (Hat, skin)
local cosmetics = script.cosmetic:Clone()
cosmetics.Parent = player
--UserId
local playerUserId = 'Player_'..player.UserId
--Assign data variable (essentially the variable that holds all things in the datastore)
local data
pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if data then
--Player has data, load it
custard.Value = data['Money']
cosmetics.customSkinBody.Value = data['customSkinBody']
cosmetics.customSkinFace.Value = data['customSkinFace']
print("Data loaded for "..player.Name)
--[[
Here is where I need to load BoolValues that aren't automatically parented
to the 'leaderstats' and 'cosmetic' folder that is given to the player above.
These values are added to the player when they buy a hat, and are saved
correctly, but I don't know how to turn the saved data back into an instance.
]]
end
end
Hopefully I worded this question in an easy to understand way lol
I’d need to access the table from multiple different scripts–so I’d need to use a ModuleScript, right? I don’t know exactly how I’d do that per-player and also save it. Unless I’m just over complicating things, which is likely. If you couldn’t tell, I’m very new to this stuff lol
local data = {
["Sword"] = true,
["Apple"] = false,
["Gun"] = true,
}
for i, v in pairs(data) do
local bool = Instance.new("BoolValue")
bool.Name = data[i]
bool.Value = v
bool.Parent = player
end
local Datastore = game:GetService("DataStoreService"):GetDataStore("Test1")
-- Saving Script --
local BoolValue = Instance.new("BoolValue")
BoolValue.Name = "TestName"
BoolValue.Value = true
local TestScope = 123
local InstanceValues = { -- Insert your instances (that have a value) into this table
BoolValue;
}
local Data = {}
for _, v in pairs(InstanceValues) do
table.insert(Data, {
Name = v.Name;
Type = v.ClassName;
Value = v.Value;
})
end
local Success, Response = pcall(function()
Datastore:SetAsync(TestScope, Data)
end)
-- Loading Data Script --
local TestScope = 123
local NewData
local Success, Response = pcall(function()
NewData = Datastore:GetAsync(TestScope)
end)
if NewData and type(NewData) == "table" then
for _, item in pairs(NewData) do
if item.Type then
local NewValue = Instance.new(item.Type)
if item.Name then
NewValue.Name = item.Name
end
if item.Value ~= nil then
NewValue.Value = item.Value
end
-- Do whatever you want below here
end
end
end
The script only supports Instance value. Feel free to change the script around according to your previous script. If you read through this, it should make sense and you should know what you need to change in the script.