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

[URGENT] I cannot save my table to DataStore?

So I am trying to make a table that saves your information, all seems to work well but it does not load the previous session's information. Here is my server script in Workspace so far:

local ds = game:GetService("DataStoreService")
local played = ds:GetDataStore("Played")
local invs = ds:GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(plr)
	local playedbefore = Instance.new("BoolValue",plr)
	playedbefore.Name = "PlayedBefore"
	playedbefore.Value = played:GetAsync(plr.UserId,playedbefore.Value) or false
	wait()
	if playedbefore.Value == true then
		local inv = {}
		invs:GetAsync(plr.UserId,inv)
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local cl = game.ReplicatedStorage:WaitForChild(inv):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(inv):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	else
		local inv = {"Sword"}
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local tool = table.unpack(inv,i,i)
				local cl = game.ReplicatedStorage:WaitForChild(tool):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(tool):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local inv = {}
	for i, v in pairs(plr.StarterGear:GetChildren()) do
		table.insert(inv,#inv,v.Name)
		v:Destroy()
	end
	wait()
	invs:SetAsync(plr.UserId,inv)
	if plr:WaitForChild("PlayedBefore").Value == false then
		played:SetAsync(plr.UserId,true)
	end
end)

There is also a part that when you touch it you get another tool. Here is it's server script:

local db = {}
script.Parent.Touched:Connect(function(hit)
	if hit:FindFirstAncestorOfClass("Model") then
		local char = hit:FindFirstAncestorOfClass("Model")
		if game.Players:GetPlayerFromCharacter(char) then
			local plr = game.Players:GetPlayerFromCharacter(char)
			if db[char] == true then
			else
				if plr.StarterGear:FindFirstChild("Iron Sword") then
				else
					db[char] = true
					local cl = game.ReplicatedStorage:WaitForChild("Iron Sword"):Clone()
					local cll = game.ReplicatedStorage:WaitForChild("Iron Sword"):Clone()
					cl.Parent = plr.Backpack
					cll.Parent = plr.StarterGear
					wait(2)
					db[char] = nil
				end
			end
		end
	end
end)

There are two empty tools I use to test this in ReplicatedStorage. One is named: "Sword" and the other is named: "Iron Sword". When I leave and join back it doesn't load my previous items.

So to conclude, everything here works except the saving of the table upon leaving. Any help would be greatly appreciated.

-PrismaticFruits

EDIT So I saw the tutorial given to me in the answers and modified my script. It still does not work. I am also wondering, why isn't the table saving when the thing below it is? I think there is something wrong with the saving/loading of the table. Anyway here is my modified script:

local ds = game:GetService("DataStoreService")
local played = ds:GetDataStore("Played")
local invs = ds:GetDataStore("Inventory")
local function load(plr)
    local playedbefore = Instance.new("BoolValue",plr)
	playedbefore.Name = "PlayedBefore"
	playedbefore.Value = played:GetAsync(plr.UserId,playedbefore.Value) or false
	wait()
	if playedbefore.Value == true then
		local inv = {}
		invs:GetAsync(plr.UserId,inv)
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local cl = game.ReplicatedStorage:WaitForChild(inv):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(inv):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	else
		local inv = {"Sword"}
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local tool = table.unpack(inv,i,i)
				local cl = game.ReplicatedStorage:WaitForChild(tool):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(tool):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	end
end

local function save(plr)
    local inv = {}
	for i, v in pairs(plr.StarterGear:GetChildren()) do
		table.insert(inv,#inv,v.Name)
		v:Destroy()
	end
	wait()
	invs:SetAsync(plr.UserId,inv)
	if plr:WaitForChild("PlayedBefore").Value == false then
		played:SetAsync(plr.UserId,true)
	end
end

game.Players.PlayerAdded:Connect(function(plr)
   load(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    save(plr)
end)
game:BindToClose(function()
    for _, client in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(save)(client)
		wait(5)
    end
end)

1 answer

It's likely it has nothing to do with table not saving but more in general. PlayerRemoving event as datastore saving is unreliable when the last player leaves. especially when u test it out in studio

playerRemoving doesnt work in the following conditions:

  • The last player leaves
  • The developer manually shut them down via the game page
  • A network error is encountered and the server is forced to shut down

see this article how to properly save

3 comments

0

Doesnt work

0

doesnt work like what ? did you test ingame

0

Use GetService for the game.Players, it happens a lot that the player spawns in before the service is active

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