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

How do you save dictionaries?

Can you do this?

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

MyDictionary = {
	FavoriteNums = {2, 4, 6, 10},
	FavoriteLetters = {"A", "B", "D"}
				}

game.Players.PlayerAdded:Connect(function(player)
	MyDictionary:GetAsync(player.UserId)
end)

game.Players.PlayerRemoving:Connect(function(player)
	MyDictionary:SetAsync(player.UserId)
end)

2 comments

0

Fuck you nigger

0

^^^ what a retard swearing

1 answer

I had trouble with this too back in the days lol. Here's an example :)

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

local sessionData = {}

game.Players.PlayerAdded:Connect(function(player)
	local key = player.UserId
	local success = false
	local tries = 0
	while not success and tries < 4 do
		success, data = pcall(function()
			return DataStore:GetAsync(player.UserId)
		end)
		tries = tries + 1
		wait(1.5 * tries)
	end
	if success then
		if data then
			sessionData[key] = data
			print("Found data for "..player.Name..".")
		else
			local data = {
				FavoriteNums = {2, 4, 6, 10},
				FavoriteLetters = {"A", "B", "D"}
			}
			sessionData[key] = data
			print("Created data for "..player.Name..".")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId
	if not sessionData[key] then return end
	local success = false
	local tries = 0
	while not success and tries < 4 do
		success = pcall(function()
			DataStore:UpdateAsync(key, function()
				return sessionData[key]
			end)
		end)
		tries = tries + 1
		wait(1.5 * tries)
	end
	if success then
		print("Saved "..player.Name.."'s data".)
	else
		print("Unable to save "..player.Name.."'s data.")
	end
	sessionData[key] = nil
end)

You should probably also give this article a read: Saving Player Data.

1 comment

0

Is this using a server or module script?

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