Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Having trouble datastoring multiple save slots, how can I do this?

I've been trying to do this for a while now, but nothing has seemed to work. I cannot think of anything else to try.

local DataStore = game:GetService("DataStoreService"):GetGlobalDataStore("SaveSlotsTest")

game.Players.PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder",Player)
	Folder.Name = "PlayerStorage"
	
	local Slot1 = Instance.new("Folder",Folder)
	Slot1.Name = "Slot1"
	
	local Slot2 = Instance.new("Folder",Folder)
	Slot2.Name = "Slot2"
	
	local Val = Instance.new("IntValue",Folder)
	Val.Name = "Test1"
	
	local Val2 = Instance.new("IntValue",Folder)
	Val2.Name = "Test2"
	
	local Val3 = Instance.new("IntValue",Folder)
	Val3.Name = "Test3"
	
	local Stats = {Val,Val2,Val3}
	
	local Slots = {Slot1,Slot2}
	
	local newstat = nil
	for i, slot in pairs(Slots) do
		for i, stat in pairs(Stats) do
			newstat = stat:Clone()
			stat:Destroy()
			newstat.Parent = slot				
		end
	end
	
	local Stats = DataStore:GetAsync(Player.UserId)
	
	if Stats ~= nil	 then
		newstat.Value = Stats[1]
	else
		newstat.Value = nil
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local SaveData = {}
	
	for i,v in pairs(Player.PlayerStorage:GetDescendants()) do

			table.insert(SaveData,v)

	end
	
	DataStore:SetAsync(Player.UserId, game:GetService("HttpService"):JSONEncode(SaveData))
end)

2 answers

Try using game:BindToClose the reason it's happening is because the server is shutdown before the actual data can be saved, what bindToClose does is it keeps the server up until that piece of code was ran. Here is an example of it being used:

game:BindToClose(function()
	print("This server is closing!")
	wait(5)
	print("Server closed!")
end)

6 comments

0

Right, should I put that outside of the PlayerRemoving function, or put it inside? and if outside, how should I define the player?

0

I added this onto the script, but it doesn't seem to work.

0

When the game is closing, loop through all the players and save their data

0

Right, I've made a script for that and it doesn't seem to be working. I'll post it here.

0

k

0

Posted.

local DataStore = game:GetService("DataStoreService"):GetGlobalDataStore("SaveSlotsTest")
local SaveData = {}

game.Players.PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder",Player)
	Folder.Name = "PlayerStorage"
	
	local Slot1 = Instance.new("Folder",Folder)
	Slot1.Name = "Slot1"
	
	local Slot2 = Instance.new("Folder",Folder)
	Slot2.Name = "Slot2"
	
	local Val = Instance.new("IntValue",Folder)
	Val.Name = "Test1"
	
	local Val2 = Instance.new("IntValue",Folder)
	Val2.Name = "Test2"
	
	local Val3 = Instance.new("IntValue",Folder)
	Val3.Name = "Test3"
	
	local Stats = {Val,Val2,Val3}
	
	local Slots = {Slot1,Slot2}
	
	local newstat = nil
	for i, slot in pairs(Slots) do
		for i, stat in pairs(Stats) do
			newstat = stat:Clone()
			stat:Destroy()
			newstat.Parent = slot				
		end
	end
	
	local Stats = DataStore:GetAsync(Player.UserId,SaveData)
	
	if Stats ~= nil	 then
		newstat.Value = Stats[1]
	else
		newstat.Value = nil
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)

	for i,v in pairs(Player.PlayerStorage:GetDescendants()) do
		if v:IsA("IntValue") then
		table.insert(SaveData,v.Value)
		end
	end
	
	local yes, no = pcall(function()
		DataStore:SetAsync(Player.UserId, game:GetService("HttpService"):JSONEncode(SaveData))
		end)
	if not yes then
		warn(no)
	else

	end
end)

game:BindToClose(function()
	
	print("saving")
	
	local players = game.Players:GetPlayers()
	
	for _,player in pairs(players) do
		
		for i,v in pairs(player.PlayerStorage:GetDescendants()) do
			if v:IsA("IntValue") then
				table.insert(SaveData,v.Value)
			end
		end
		
		local userID = player.UserId
			local yes, no = pcall(function()
				DataStore:SetAsync(userID,game:GetService("HttpService"):JSONEncode(SaveData))
			end)
			if not yes then
				warn(no)
			else
				
			end
	end
	print("saved")
end)

I've added some things onto the older script, so I sent the full thing and to just the BindToClose. This looks rights to me.

6 comments

0

Is there an error?

0

I got this warning.

0

Why are you using a global data store

0

This is the error: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 147548657. And I'm using a global data store because I have many places in the game.

0

I just added a print on the GetAsync part, and it prints the values from when I left, but the values don't actually change.

0

I think I know what's happening, the script doesn't know which value to change to the one the datastore has saved.

This discussion is preserved as it was. New answers, comments, and votes are closed.