I’m trying to make a script which saves multiple booleans all with values within them, this boolean is created when you purchase an item from a shop. Ff the boolean exists, the item is owned, and if the boolean is true, then it is added to the owner’s Backpack + StarterGear when they join.
The issue is that I do not know how to save a boolean w/ it’s value within a table on the fly
-- JOINING
local ds = game:GetService("DataStoreService"):GetDataStore("toolsToSave")
game.Players.PlayerAdded:connect(function(plr)
local ToolsFolder = Instance.new("Folder", plr)
ToolsFolder.Name = "ToolsSave"
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.AllItems:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
local ToolBool = Instance.new("BoolValue", ToolsFolder)
end
end
end
end)
end)
-- LEAVING
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
for i,v in pairs(plr.ToolsSave:GetChildren()) do
if v then
toolsToSave[v.Name] = v.Value
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
NOTE: I’m ALMOST 100% sure that the “LATER” part of the script works, however I’m not 100% sure (kinda like I said earlier)
This is correct. You shouldn’t use :GetAsync() so often. It likely won’t crash your script but you will reach the max limit for requests if you have too many values to loop through. I know you’re asking for help on this specific problem, but in general, a module like ProfileService or Datastore2 (or your own if you know what you’re doing) is a much more efficient and user-friendly way to store data without running into datastore queue problems.
EDIT:
Looking through this again, the later part of the script should work.
The first part of the script should not.
Did you assign a name to the ToolBool? otherwise it won’t be able to find the item. It looks like it should be equivalent to the tool’s name in AllItems. Also, a bool should be created whether or not it finds it in the datastore - I’m guessing it should be set to false by default, and then it should only give you the item if it’s true.
I don’t really like explaining to beginners to use DataStore2 or ProfileService, because once they use those they have no idea what happens to their data, and although its the safest way to protect your data if you’re really trying to learn lua the most you can I suggest making your own Datastore. Also having good knowledge of Datastores can make for complex and smart inventions with your own game