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

Data not saving, getting "error when saving data" ?

local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetOrderedDataStore("PlayerData")
local teams = game:GetService("Teams")
local free = teams.Free
local leaderboard = workspace:FindFirstChild("Leaderboard")
local updateEvent = leaderboard:FindFirstChild("UpdateEvent")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local arrests = Instance.new("IntValue")
	arrests.Value = 0
	arrests.Name = "Arrests"
	arrests.Parent = leaderstats
	
	local arrested = Instance.new("IntValue")
	arrested.Value = 0
	arrested.Name = "Arrested"
	arrested.Parent = leaderstats
	
	local playerUserId = "Player_"..plr.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		arrests.Value = data["Arrests"]
		arrested.Value = data["Arrested"]
	--	plr.Team = data["Team"]
	--	print(plr.Team)
	else
		arrests.Value = 0
		arrested.Value = 0
	--	plr.Team = free
	--	print("put on free team")
	end
end)

local function createTable(player)
	local playerStats = {}
	for i,stat in pairs(player.leaderstats:GetChildren()) do
		playerStats[stat.Name] = stat.Value
	end
	--playerStats["Team"] = player.Team.Name
	return playerStats
end

local function onPlayerExit(player)
	local playerStats = createTable(player)
	print(playerStats)
	local success, errorMessage = pcall(function()
		local playerUserId = "Player_"..player.UserId
		print(playerUserId)
		playerData:SetAsync(playerUserId,playerStats)
	end)
	if not success then
		warn("error when saving data")
	end
end

game.Players.PlayerRemoving:Connect(onPlayerExit)

--game:BindToClose(onPlayerExit)

while true do
	for i,v in ipairs(game.Players:GetChildren()) do
		local leaderstats = v:FindFirstChild("leaderstats")
		if leaderstats then
			local arrests = leaderstats:FindFirstChild("Arrests")
			if arrests then
				local success, err = pcall(function()
					playerData:SetAsync(v.Name,arrests.Value)
				end)
			end
		end
	end
	updateEvent:Fire()
	wait(60)
end

In the game, the player's arrests and arrested values are saved, and then put on a leaderboard for the top 5 player's in the game (the while true do loop). However the player's data is just not saving. I was thinking maybe it had something to do with playerData:SetAsync(v.Name,arrests.Value) and playerData:SetAsync(playerUserId,playerStats)

being a bit different in the way data is saved but i could be wrong.

1 comment

0

b

1 answer

I ran your script and the errorMessage was Dictionary is not allowed in data stores.

I found this post on the Roblox devforum, where they used HttpService:JSONEncode and JSONDecode. (As they said in the post, your data could be lost)

You can use JSONEncode with SetAsync, and JSONDecode with GetAsync. The post also says how you can create the same stats(Instances - IntValue, etc)you had before. You would need to do something like this

First of all enable HttpService in your game by running this: game:GetService("HttpService").HttpEnabled = true

local HttpService = game:GetService("HttpService")

--saving
local success, errorMessage = pcall(function()
	local playerUserId = "Player_"..player.UserId
	playerData:SetAsync(playerUserId,HttpService:JSONEncode(playerStats))
end)


--reading
local data = HttpService:JSONDecode(HttpService:GetAsync(playerUserId))
for k,v in pairs(data) do
	local i = Instance.new("IntValue") --obviously this only works if every stat was an int. If not, you need to save the class name as well.
	i.Name = k
	i.Value = v
	i.Parent = player.leaderstats
end

(I didn't test this code)

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