How do I correct get the last value of a table?

I’m attempting to create a system of logging point values from a game to a website. In order to get a valid JSON to upload to the site, I need to check if the current value I’m iterating through in a table is the last one. I have this function:

function isLastValue(tab)
	for i,v in next,tab do
		if tab[table.find(tab,i)+1] ~= nil then
			return true
	return false

Yet that returns: Attempt to perform arithmetic (add) on nil

My current code to iterate through the and format a JSON string

		local ReturnCode = nil
		local FinalTable = {}
		local FinalJSON = ""
		--//Insert all users to the FinalTable
		for i,v in pairs(OnTime) do
			FinalTable[i] = v
		for i,v in pairs(Late) do
			FinalTable[i] = v
		FinalJSON = FinalJSON..'{'
		for key,value in pairs(FinalTable) do
			local name = game.Players:GetNameFromUserIdAsync(tonumber(key))
			if isLastValue(FinalTable,key,value) then
				FinalJSON = FinalJSON..'"''": {"UserID":"'..key..'", "Points":"'..value..'"}'
				FinalJSON = FinalJSON..'"''": {"UserID":"'..key..'", "Points":"'..value..'"},'
		FinalJSON = FinalJSON..'}'

Been stumped on this for awhile. Anyone know where I messed up?

You’re giving table.find an index. You may be thinking of table.remove which works that way. However the former takes a value. So replace i with v.

What is the point of this? Why reinvent the wheel? Can’t you just use HttpService:JSONEncode?

1 Like

Oh, I didn’t think HTTP:JSONEncode was able to operate on tables, I thought it could just use JSON strings, haha. I changed what you said and it worked! :smiley: Tysm ^w^