DataStore Saving Certain Values as "nil"

No need for an explanation. My DataStore is simply saving some values or “0” or “false” as nil.

Script:

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

local function SaveData(Player)
	local TableToSave = {
		Player.Items.FishingWire.Value;
		Player.Items.WateringCan.Value;
		Player.Items.Fertilizer.Value;
		Player.Items.Shovel.Value;

		--Trails--

		Player.Items.Trails.TrailRed.Value;
		Player.Items.Trails.TrailRed:GetAttribute("Equipped");
		Player.Items.Trails.TrailOrange.Value;
		Player.Items.Trails.TrailOrange:SetAttribute("Equipped");
		Player.Items.Trails.TrailYellow.Value;
		Player.Items.Trails.TrailYellow:SetAttribute("Equipped");
		Player.Items.Trails.TrailLime.Value;
		Player.Items.Trails.TrailLime:SetAttribute("Equipped");
		Player.Items.Trails.TrailGreen.Value;
		Player.Items.Trails.TrailGreen:SetAttribute("Equipped");
		Player.Items.Trails.TrailCyan.Value;
		Player.Items.Trails.TrailCyan:SetAttribute("Equipped");
		Player.Items.Trails.TrailBlue.Value;
		Player.Items.Trails.TrailBlue:SetAttribute("Equipped");
		Player.Items.Trails.TrailPurple.Value;
		Player.Items.Trails.TrailPurple:SetAttribute("Equipped");
		Player.Items.Trails.TrailPink.Value;
		Player.Items.Trails.TrailPink:SetAttribute("Equipped");
		Player.Items.Trails.TrailVIP.Value;
		Player.Items.Trails.TrailVIP:SetAttribute("Equipped");

		--Sparkles

		Player.Items.Effects.SparklesRed.Value;
		Player.Items.Effects.SparklesRed:GetAttribute("Equipped");
		Player.Items.Effects.SparklesOrange.Value;
		Player.Items.Effects.SparklesOrange:SetAttribute("Equipped");
		Player.Items.Effects.SparklesYellow.Value;
		Player.Items.Effects.SparklesYellow:SetAttribute("Equipped");
		Player.Items.Effects.SparklesLime.Value;
		Player.Items.Effects.SparklesLime:SetAttribute("Equipped");
		Player.Items.Effects.SparklesGreen.Value;
		Player.Items.Effects.SparklesGreen:SetAttribute("Equipped");
		Player.Items.Effects.SparklesCyan.Value;
		Player.Items.Effects.SparklesCyan:SetAttribute("Equipped");
		Player.Items.Effects.SparklesBlue.Value;
		Player.Items.Effects.SparklesBlue:SetAttribute("Equipped");
		Player.Items.Effects.SparklesPurple.Value;
		Player.Items.Effects.SparklesPurple:SetAttribute("Equipped");
		Player.Items.Effects.SparklesPink.Value;
		Player.Items.Effects.SparklesPink:SetAttribute("Equipped");
		Player.Items.Effects.SparklesVIP.Value;
		Player.Items.Effects.SparklesVIP:SetAttribute("Equipped");
	}

	local success, errorMessage = pcall(DataStore.SetAsync, DataStore, Player.UserId, TableToSave)

	if success then
		--print("Player data saved for "..Player.Name.."")
	else
		--print("Player data not saved for "..Player.Name.."!")
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait()

	local Leaderstats = Player.Leaderstats

	local Data = nil

	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)

	if success and Data then
		Player.Items.FishingWire.Value = Data[1]
		Player.Items.WateringCan.Value = Data[2]
		Player.Items.Fertilizer.Value = Data[3]
		Player.Items.Shovel.Value = Data[4]

		--Trails--

		Player.Items.Trails.TrailRed.Value = Data[5]
		Player.Items.Trails.TrailRed:GetAttribute("Equipped", Data[6])
		Player.Items.Trails.TrailOrange.Value = Data[7]
		Player.Items.Trails.TrailOrange:SetAttribute("Equipped", Data[8])
		Player.Items.Trails.TrailYellow.Value = Data[9]
		Player.Items.Trails.TrailYellow:SetAttribute("Equipped", Data[10])
		Player.Items.Trails.TrailLime.Value = Data[11]
		Player.Items.Trails.TrailLime:SetAttribute("Equipped", Data[12])
		Player.Items.Trails.TrailGreen.Value = Data[13]
		Player.Items.Trails.TrailGreen:SetAttribute("Equipped", Data[14])
		Player.Items.Trails.TrailCyan.Value = Data[15]
		Player.Items.Trails.TrailCyan:SetAttribute("Equipped", Data[16])
		Player.Items.Trails.TrailBlue.Value = Data[17]
		Player.Items.Trails.TrailBlue:SetAttribute("Equipped", Data[18])
		Player.Items.Trails.TrailPurple.Value = Data[19]
		Player.Items.Trails.TrailPurple:SetAttribute("Equipped", Data[20])
		Player.Items.Trails.TrailPink.Value = Data[21]
		Player.Items.Trails.TrailPink:SetAttribute("Equipped", Data[22])
		Player.Items.Trails.TrailVIP.Value = Data[23]
		Player.Items.Trails.TrailVIP:SetAttribute("Equipped", Data[24])

		--Sparkles

		Player.Items.Effects.SparklesRed.Value = Data[25]
		Player.Items.Effects.SparklesRed:SetAttribute("Equipped", Data[26])
		Player.Items.Effects.SparklesOrange.Value = Data[27]
		Player.Items.Effects.SparklesOrange:SetAttribute("Equipped", Data[28])
		Player.Items.Effects.SparklesYellow.Value = Data[29]
		Player.Items.Effects.SparklesYellow:SetAttribute("Equipped", Data[30])
		Player.Items.Effects.SparklesLime.Value = Data[31]
		Player.Items.Effects.SparklesLime:SetAttribute("Equipped", Data[32])
		Player.Items.Effects.SparklesGreen.Value = Data[33]
		Player.Items.Effects.SparklesGreen:SetAttribute("Equipped", Data[34])
		Player.Items.Effects.SparklesCyan.Value = Data[35]
		Player.Items.Effects.SparklesCyan:SetAttribute("Equipped", Data[36])
		Player.Items.Effects.SparklesBlue.Value = Data[37]
		Player.Items.Effects.SparklesBlue:SetAttribute("Equipped", Data[38])
		Player.Items.Effects.SparklesPurple.Value = Data[39]
		Player.Items.Effects.SparklesPurple:SetAttribute("Equipped", Data[40])
		Player.Items.Effects.SparklesPink.Value = Data[41]
		Player.Items.Effects.SparklesPink:SetAttribute("Equipped", Data[42])
		Player.Items.Effects.SparklesVIP.Value = Data[43]
		Player.Items.Effects.SparklesVIP:SetAttribute("Equipped", Data[44])
	else
		--print("No player data found for "..Player.Name.."!")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

game:BindToClose(function()
	for _, Player in ipairs(game.Players:GetPlayers()) do
		task.spawn(SaveData, Player)
	end
end)

image

could you let us know what lines the errors are on?

The first one start on Player.Items.Effects.SparklesYellow.Value = Data[29] (line 113) and goes on for every line of Sparkles.(color).Value = Data[(number)] until the end of sparkles. For some reason there are no errors for the "SetAttribute"s.

I think the problem is that because no data has been saved yet, all of the indexs in data are nil, and obviously as the error states, you can’t convert nil to a number. I don’t know if this works with datastores, but it does work with normal tables; you could use the “or” operator, e.g.:

local data = {1, 4, 3}
local newData = {}

newData[1] = data[6] or 3

print(newData)

This script prints an array where the first index is 3, as data[6] doesn’t exist. In your case if data[index] is nil then use or to set a default value instead.

Thanks for the help. I got busy all of a sudden so I’ll try it out later and let you know if it worked.

So, it doesn’t show an error anymore, but when I set the values of sparkles yellow and after to 1 from server side, they still save as 0, because of the “or”. Ttyl.