How would I make a Table Datastore that holds multiple values without sending to many datastore requests? Sorry I’m not very good with tables and datastores need some help with this
For example, heres a script I made that saves an IntValue, it works but I want to make multiple that saves under a table instead. Any help appreciated
local dsService = game:GetService(“DataStoreService”)
local ds = dsService:GetDataStore(“Skins”)
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new(“Folder”, plr)
folder.Name = “skins”
local skin1 = Instance.new("IntValue", folder)
skin1.Name = "NoobSkin"
local data
local success, errormessage = pcall(function()
data = ds:GetAsync(plr.UserId.."-NoobSkin") or 1
end)
if success then
skin1.Value = data
print("Successfully loaded Skin data")
else
print("Error loading data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
ds:SetAsync(plr.UserId…"-NoobSkin", plr.skins.NoobSkin.Value)
end)
if success then
print("Succesfully saved Skin data")
else
print("Error saving Skin data")
warn(errormessage)
end
end)
Heres a screenshort instead cause the code looks funny
You need to get the skins inside of the folder which for i,v in pairs and use get children then use table.insert() and insert them into the data table inside of player removing and then you can add in v.Name to the table and then create an instance.new(value u want to create) for the skins in the folder.
game.Players.PlayerAdded:Connect(function(plr)
local LoadedData
local success, errorMessage = pcall(function()
LoadedData = ds:GetAsync(player.UserId)
end)
if LoadedData ~= nil then
if data.SkinsSavedthen
for i,v in pairs(data.SkinsSaved) do
local stringVal = Instance.new("StringValue")
stringVal.Name = v
stringVal.Parent = skins
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local DataToSave = {}
DataToSave.SkinsSaved = {}
for i,v in pairs(plr.skins:GetChildren()) do
table.insert(DataToSave.SkinsSaved,v.Name)
end
local success,errorMessage = pcall(function()
ds:SetAsync(player.UserId,DataToSave)
end)
end)
@Rynappel ^^^ look at the code that I sent you. Also just to clarify you can’t ask for free code. I fixed it by the way and if it works out you can mark it as a solution
I had detailed where you include each parts that I gave you. I have edited it to fit your variable names though. You just need to replace any Asyncs you have already with the code I sent you.
Edited it again, so if you had already copied it, you might need to again.
This is what I have:
local dsService = game:GetService(“DataStoreService”)
local ds = dsService:GetDataStore(“Skins”)
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new(“Folder”, plr)
folder.Name = “skins”
local skin1 = Instance.new("IntValue", folder)
skin1.Name = "NoobSkin"
local skin2 = Instance.new("IntValue", folder)
skin2.Name = "BussinessManSkin"
local data
local success, errormessage = pcall(function()
data = ds:GetAsync(plr.UserId)
end)
if data then
skin1.Value = data.NoobSkin
skin2.Value = data.BussinessManSkin
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = {
skin1 = plr.skins.NoobSkin.Value;
skin2 = plr.skin.BussinessManSkin.Value
}
local success, errormessage = pcall(function()
ds:SetAsync(plr.UserId, data)
end)
end)
If you are testing it in studio, it will NOT work.
As i see it, data stores most of the time DO NOT SAVE via studio, you should try it out in the actual place, as the help you’ve received so far is all correct.
Either way, good luck with your issue!
I do not see what could be the issue then.
Try testing it in studio and see if there are any specific errors that show up in the output console? Using what these devs provided, of course that is.
Pretty sure this problem is still happening due to the server being shut down if no players are left in a server. Like what @DarkDanny04 posted, you should add a game:BindToClose function at the end of the script to kick all players. Here:
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
player:Kick()
end
end