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

How do I get datastore to save arrays?

I'm trying to save an array in the datastore but when I try to set a variable as the saved array, the values return as nil. Can someone tell me what I'm doing wrong?

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

game.Players.PlayerAdded:connect(function(plr)
	local key = "id-"..plr.UserId
	local saves = ds:GetAsync(key)

	local points = Instance.new("NumberValue", plr)
	points.Name = "Points"
	points.Value = 0
	local class = Instance.new("StringValue", plr)
	class.Name = "Class"
	class.Value = "Fighter"
	local color = Instance.new("StringValue", plr)
	color.Name = "Color"
	color.Value = "Bright green"
	
	points.Value = saves[1]
	class.Value = saves[2]
	color.Value = saves[3]
end)

game.Players.PlayerRemoving:connect(function(plr)
	local key = "id-"..plr.UserId
	local savearray = {plr.Points.Value, plr.Class.Value, plr.Color.Value}
	ds:SetAsync(key,savearray)
end)

1 comment

0

I see nothing wrong with this script... Weird..

1 answer

The first retrieved data will always be nil since you never saved. Prepare for all data to be nil. i.e. local saves = ds:GetAsync(key) or {}

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