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

Script not saving player's data (tables) when player leaves?

Okay so I have this DataStore script that uses tables:

local KeyData = {
	Key = "19AGS-JGCVM-IO9MK2",
	Version = "1"
}
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("Key: "..KeyData.Key..", Version: "..KeyData.Version)

local PubData = {
	["Coins"] = 0,
	["Kills"] = 0,
	["Level"] = 0
}
local ObjectData = {
	["Class"] = "Warrior"
}
local NumberData = {
	["Episode"] = 0,
	["xpReq"] = 1000,
	["xpProg"] = 0
}
game.Players.PlayerAdded:Connect(function(player)
	local DataFromStore = nil
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	for n,v in pairs(PubData) do
		local new = Instance.new("IntValue")
		new.Name = n
		new.Value = v
		new.Parent = leaderstats
	end
	for n,v in pairs(ObjectData) do
		local new = Instance.new("StringValue")
		new.Name = n
		new.Value = v
		new.Parent = player
	end
	for n,v in pairs(NumberData) do
		local new = Instance.new("NumberValue")
		new.Name = n
		new.Value = v
		new.Parent = player
	end
	
	local suc, err = pcall(function()
		DataFromStore = DataStore:GetAsync(player.UserId)
	end)
	
	if suc then
		print("Getting Data For: " .. player.Name)
	elseif err then
		warn("Error Getting Data For: " .. player.Name)
	end
	
	if DataFromStore then
		for n,v in pairs(DataFromStore) do
			player.leaderstats[n].Value = v
			player.n.Value = v
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local DataForStore = {}
	
	for name, value in pairs(player.leaderstats:GetChildren()) do
		table.insert(DataForStore, value)
		DataForStore[value.Name] = value.Value
		print(unpack(DataForStore))
	end
	
	DataStore:SetAsync(player.UserId, DataForStore)
end)

The script works well except for this part:

game.Players.PlayerRemoving:Connect(function(player)
	local DataForStore = {}
	
	for name, value in pairs(player.leaderstats:GetChildren()) do
		table.insert(DataForStore, value)
		DataForStore[value.Name] = value.Value
		print(unpack(DataForStore))
	end
	
	DataStore:SetAsync(player.UserId, DataForStore)
end)

I'm getting the error of that you cannot store Arrays (tables) in DataStores and it can only accept valid UTF-8 characters. How do I fix this? Keep in mind it's just this part that has a problem and it's supposed to save player's data as normal. To understand, DataForStore is a table variable that temporarily holds player data so it can save and load data. DataStore is a variable that has a key as Data to refer as so I can reset it whenever I want.

2 answers

Looks like you got this code from this thread if you read the comments you would know u have the same problem, remove the table.insert(DataForStore, value) and it will work, it errors bc u trying to save an table with instances, you can only save numbers and strings

There are 3 reasons why using PlayerRemoving event will not work when saving a player:

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

when ur testing in studio without server with mutliple players always the first issue happens

This article explains how u should save when those actions arise

6 comments

0

Yes, I configured it to my preferred settings. I’ll try this solution out when I’m able to get on.

0

Removing that line did nothing but remove the error. My API services are also on

0

More specifically, the player data does not save. I put a print after line 74 but it doesn't print anything

0

Exactly that removed the UTF-8 error, ill edit my anwser to explain why its not saving

0

Ah right, I understand now. I'll see if I can do this with multiple players.

0

You can mark it now as awsner

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