Help with a datastore table error: ServerScriptService.CurrencyScript:61: attempt to index nil with 'SetAsync'

So I’ve come across an error with a datastore table recently, and I think I know the issue, but I’m not so sure. But basically, what I’m wondering is if it is possible to store a datastore in a table and then when you need the value of that index of the table, you can set the async. But, I don’t know why it’s not working in my code. Here’s my code below:

Btw, the error is at this line: datastore:SetAsync(playerKey, false)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local currencyName = "Coins"
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local DataStoreService = game:GetService("DataStoreService")
local TrailForDatastoreEvent = Events:WaitForChild("TrailForDatastoreEvent")

local datastoretable = {DataStoreService:GetDataStore("Trails"), DataStoreService:GetDataStore("Trails2"), DataStoreService:GetDataStore("Trails3"), DataStoreService:GetDataStore("Trails4"), DataStoreService:GetDataStore("Trails5"), DataStoreService:GetDataStore("Trails6"), DataStoreService:GetDataStore("Trails7"), DataStoreService:GetDataStore("Trails8"), DataStoreService:GetDataStore("Trails9"), DataStoreService:GetDataStore("Trails10"), DataStoreService:GetDataStore("Trails11", DataStoreService:GetDataStore("Trails12")), DataStoreService:GetDataStore("Halos"), DataStoreService:GetDataStore("Halos1"), DataStoreService:GetDataStore("Halos2"), DataStoreService:GetDataStore("Halos3"), DataStoreService:GetDataStore("Halos4"), DataStoreService:GetDataStore("Halos5"), DataStoreService:GetDataStore("Halos6"), DataStoreService:GetDataStore("Halos7"), DataStoreService:GetDataStore("Halos8"), DataStoreService:GetDataStore("Halos9")}
	function trailwhatdoicallthisfunction(player, index)
		print("it is set to false")
		local playerKey = "Player_" .. player.UserId
	local datastore = datastoretable[index]
	datastore:SetAsync(playerKey, false)
	end

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	local savedData = nil	
	
	local trailandhaloandnametagdatatable = {"redtraildata", "orangetraildata", "yellowtraildata", "greentraildata", "bluetraildata", "purpletraildata", "aquatraildata", "pinktraildata", "whitetraildata", "rainbowtraildata", "firetraildata", "oceantraildata", "redhalodata", "orangehalodata", "yellowhalodata", "greenhalodata", "bluehalodata", "purplehalodata", "aquahalodata", "pinkhalodata", "whitehalodata"}
	for i, v in pairs(trailandhaloandnametagdatatable) do
		trailandhaloandnametagdatatable[i] = nil
	 pcall(function()
			savedData = DataStore:GetAsync(ID)
			trailandhaloandnametagdatatable[i] = datastoretable[i]:GetAsync(playerKey)
		end)
	end

	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 10
		print("New player to the game")
	end
	local definingtrailfordatastoreevent2eventstable = {Events:WaitForChild("RedTrailForDatastoreEvent2"), Events:WaitForChild("OrangeTrailForDatastoreEvent2"), Events:WaitForChild("YellowTrailForDatastoreEvent2"), Events:WaitForChild("GreenTrailForDatastoreEvent2"), Events:WaitForChild("BlueTrailForDatastoreEvent2"), Events:WaitForChild("PurpleTrailForDatastoreEvent2"), Events:WaitForChild("AquaTrailForDatastoreEvent2"), Events:WaitForChild("PinkTrailForDatastoreEvent2"), Events:WaitForChild("WhiteTrailForDatastoreEvent2"), Events:WaitForChild("RainbowTrailForDatastoreEvent2"), Events:WaitForChild("FireTrailForDatastoreEvent2"), Events:WaitForChild("OceanTrailForDatastoreEvent2"), Events:WaitForChild("RedHaloForDatastoreEvent2"), Events:WaitForChild("OrangeHaloForDatastoreEvent2"), Events:WaitForChild("YellowHaloForDatastoreEvent2"), Events:WaitForChild("GreenHaloForDatastoreEvent2"), Events:WaitForChild("BlueHaloForDatastoreEvent2"), Events:WaitForChild("PurpleHaloForDatastoreEvent2"), Events:WaitForChild("AquaHaloForDatastoreEvent2"), Events:WaitForChild("PinkHaloForDatastoreEvent2"), Events:WaitForChild("WhiteHaloForDatastoreEvent2")}
	local trailfordatastoreevent2eventstable = {"RedTrailForDatastoreEvent2", "OrangeTrailForDatastoreEvent2", "YellowTrailForDatastoreEvent2", "GreenTrailForDatastoreEvent2", "BlueTrailForDatastoreEvent2", "PurpleTrailForDatastoreEvent2", "AquaTrailForDatastoreEvent2", "PinkTrailForDatastoreEvent2", "WhiteTrailForDatastoreEvent2", "RainbowTrailForDatastoreEvent2", "FireTrailForDatastoreEvent2", "OceanTrailForDatastoreEvent2", "RedHaloForDatastoreEvent2", "OrangeHaloForDatastoreEvent2", "YellowHaloForDatastoreEvent2", "GreenHaloForDatastoreEvent2", "BlueHaloForDatastoreEvent2", "PurpleHaloForDatastoreEvent2", "AquaHaloForDatastoreEvent2", "PinkHaloForDatastoreEvent2", "WhiteHaloForDatastoreEvent2"}
	for i, v in pairs(trailandhaloandnametagdatatable) do
		if i ~= nil then
			definingtrailfordatastoreevent2eventstable[i]:FireClient(player)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	local playerKey = "Player_" .. player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
	
end)

game:BindToClose(function()

	-- When game is ready to shutdown

	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end

	wait(5)	

end)

TrailForDatastoreEvent.OnServerEvent:Connect(trailwhatdoicallthisfunction)

Your error is what it says, your trying to call a Datastore function on a nil value. Your problem is with local datastore = datastoretable[index]. For whatever reason, the index your trying to access doesn’t exist.

Most likely this is do with your Remote Event and what is being sent from the Client to the Server.