Attempt To Index Error. I Need Help On What's Going On

Hi, my name is baccon. I’m trying to make a system where you can save tickets, trade tickets, add tickets, and remove tickets but I’m having an bit of an issue. So every time I try to call the event I get an error:

ServerScriptService.PlayerMain.136: Attempt To Index Nil 'Folder1'

I’m very confused on what could be happening. Could any one help me?

Here’s The Code.

--// Get Services
local Lighting = game:GetService( 'Lighting' )
local ReplicatedStorage = game:GetService( 'ReplicatedStorage' )
local ServerStorage = game:GetService( 'ServerStorage' )
local Teams = game:GetService( 'Teams' )
local Market = game:GetService('MarketplaceService')
local teleportService = game:GetService("TeleportService")
local DataStoreService = game:GetService("DataStoreService")

--// Define Variables
local Admins = {0, 0}
local Banned = {0}
local GameValues = workspace:WaitForChild( 'GameValues' )
local Field = workspace:WaitForChild( 'Locker' )
local RemoteEvent = Instance.new( 'RemoteEvent', ReplicatedStorage )
local RemoteFunction = Instance.new( 'RemoteFunction', ReplicatedStorage )
local Event = Instance.new("RemoteEvent", ReplicatedStorage)
Event.Name = "Event"
--// DataStoreService
local Players = game:GetService("Players")
local TicketsData = DataStoreService:GetDataStore("TicketsData1")
local ServerData = {}
local function NewData()
	return {
		Folder1 = {
			Tickets = 0,
		},

	}
end
local function LoadData(Player)
	local Data
	local Success, Error = pcall(function() Data = TicketsData:GetAsync("Data:"..Player.UserId) end)
	if not Success then
		warn("Could not get data for: "..Player.Name)
	end
	if not Data then
		Data = NewData()
	end
	ServerData[Player] = Data
end
local function SaveData(Player)
	if ServerData[Player] then
		local Success, Error = pcall(function() TicketsData:SetAsync("Data:"..Player.UserId, ServerData[Player]) end)
		if not Success then
			warn("Could not save data for: "..Player.Name)
			return
		end
		ServerData[Player] = nil
	end
end
--// function
function OnList(List, UserId)
	for _,v in pairs(List) do
		if v == UserId then
			return true
		end
	end
	return false
end

function Countdown()
	
end
function Caught_Exploiting()
	--// Catching Exploiter Messing With Data Or Bypassing Countdown
	
end

function searchPlayer(player,id)
	local success, wasFound, errorMessage, placeId, jobId = pcall(function()
		return teleportService:GetPlayerPlaceInstanceAsync(id)
	end)
	if success then
		return placeId, jobId
	else
		return "Error"
	end
end
--// Main Scripting

--// Player Aadded
game.Players.PlayerAdded:Connect(function(Plr)
	wait()
	LoadData(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		Char.Humanoid.WalkSpeed = 0
		
	end)
	if OnList(Banned, Plr.UserId) then
		print(Plr.Name..' Is On The Banned List, Please Contact OFA staff to be unbanned.')
		Plr:Kick(Plr.Name..' Is On The Banned List, Please Contact OFA staff to be unbanned.')
		--table.insert(Plr.UserId, Banned)
	elseif not OnList(Banned, Plr.UserId) then
		print('Clean')
	end
end)
--// Player Removed
game.Players.PlayerRemoving:Connect(function(Plr)
	SaveData(Plr)
end)
	
--
game:BindToClose(function()
	for _,Players in pairs(game.Players:GetPlayers()) do
		SaveData(Players)
	end
end)
--// Player Purcahsing 
Market.ProcessReceipt = function(info)
	if info.ProductId == 1108007995 then
		local Playeeee = game.Players:GetPlayerByUserId(info.PlayerId, "15")
		print('Thingy')
		ServerData[Playeeee].Folder1.Tickets = ServerData[Playeeee].Folder1.Tickets + 1
		print(ServerData[Playeeee].Folder1.Tickets)
		return Enum.ProductPurchaseDecision.PurchaseGranted 
	end
	
end
--// Remote RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(Plr, Input)
	if Input[1] == 'CaughtSlippin' then
		--Exploiting Their Way Into A Ticket
	elseif Input[1] == 'SearchForPlayer' then
		local ID = Input[2]
		searchPlayer(Plr, ID)
	elseif Input[1] == 'StarbowlCountdownFinished' then
		print('Starbowl Open')
	end	
end)
--// Remote Functions
RemoteFunction.OnServerInvoke = function(Plr, Input)
	if Input[1] == 'AddTickers' then
		local Amount = Input[2]
		local Player = Input[3]
		ServerData[Player].Folder1.Tickets = ServerData[Player].Folder1.Tickets + Amount
		print(Player, ServerData[Player].Folder1.Tickets)
	elseif Input[1] == 'RemoveTickets' then
		local Amount = Input[2]
		local Player = Input[3]
		ServerData[Player].Folder1.Tickets = ServerData[Player].Folder1.Tickets - Amount
		print(Player, ServerData[Player].Folder1.Tickets)
	elseif Input[1] == 'ReturnTicketsValue' then

	end
	
end

What is Input[3]?

It seems like you are either sending their names instead of the Player instance, or you just missmatched the numbers in the array.

A simple check would be to add this

print(typeof(Player),Player)

After

local Player = Input[3]

Are you sure it says attempt to index nil 'Folder1' and not attempt to index nil *with* 'Folder1'?

I’m guessing it does say the latter.

That means that ServerData[Player] does not exist.

Solution

  1. Make sure that local Player = Input[3] is valid (add some print calls to help)
  2. Use the lua debugger to help step through your RemoteFunction.OnServerInvoke line-by-line and check that all the values are what you expect from the client.
  3. Why are you using Input[3] instead of Plr directly?
  4. Just FYI, you don’t need to package RemoteFunction arguments in an array like that. You can use any number of arguments, as long as the first is the player:
    RemoteFunction.OnServerInvoke = function(player, Amount, Player)
    (but again, the second Player argument is probably unnecessary in this case)

I’m using Player[3] because, the players are allowed to search for the player and gift that player a ticket. I used the remote function as a way to get the data from the table.

Cool. Still recommend doing 1 and 2.