Trying to Make Table Datastore

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 :tired_face:

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 :grin:

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 image

1 Like

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.

Could you show me an example? :grinning:

I will hold on for a bit while I type it out.

1 Like

You would want to have it create a table when the player is removed, and retrieve that table when the player joins. It would look something like this

This is in the playeradded

local success, errormessage = pcall(function()
	data = myDSS:GetAsync(plr.UserId)
end)
if data then 
	skin1.Value = data.NoobSkin
end

This would be playerremoving

local data = { 
	skin1 = plr.skins.NoobSkin.Value
}
local success, errormessage = pcall(function()
	myDSS:SetAsync(plr.UserId, data)
end)

There’s obviously other ways to go about this, but this is the way I do it, and it is very reliable.

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)

Hey could you implement this into the code I’ve shown? Would be very appreciated :slight_smile:

@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

1 Like

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.

1 Like

Trying this out, thank you!! :grin:

Gonna try this out. Thanks!!! :wink:

Hey @DarkDanny04, I tested it it didn’t work :thinking:

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)

You set it up right, but I believe you are testing it in studio. You should add this at the end of your script.

game:BindToClose(function()
    print("Game is closing")
    wait(3)
    print("Game is closed") 
end)

And you can keep the debugging for your pcalls. So, just include these in the functions, but outside of the pcalls.

if success then
    print("Data loaded/saved correctly")
else
    warn(errormessage)
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 havent been testing it in studio ive been testing it in the ACTUAL game.

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? :thinking:
Using what these devs provided, of course that is.

Nah Ive been using the ACTUAL game to test it, I added back in the debug thing still no worky

Add the debug if success into the playerremoving and see if that warns anything.

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

The PlayerAdded debug printed but not the PlayerRemoving :thinking:image