Cant compare source in for statement

Explanation:
For some weird reason if i compare source with a number in a for loop it doesn’t work.
here is the code i was using.

code

Server:

local players = {}

AddEventHandler('playerDropped', function (reason)
	for i = 1, #players, 1 do
		local number = players[i].id
		print('id' .. number .. " source" .. source)
		if number == source then
			table.remove(players, i)
		end
	end
end)

Citizen.CreateThread(function()
	while true do
		for k, v in ipairs(GetPlayers()) do
			if GetPlayerPed(v) ~= 0 then
				if #players > 0 then
					for i = 1, #players, 1 do
						if players[i].id == v then
							if players[i].callback == false and not players[i].skip then
								kick(v)
								break
							elseif players[i].callback == true and not players[i].skip then
								players[i].callback = false
							elseif players[i].skip then
								players[i].skip = false
								break
							end
						else
							table.insert(players, {name = GetPlayerName(v), id = v, callback = false, skip = true, connecting = false})
							break
						end
					end
				else
					table.insert(players, {name = GetPlayerName(v), id = v, callback = false, skip = true, connecting = false})
				end
				TriggerClientEvent('heartbeat:Client', v, v)
			end
		end
		Citizen.Wait(500)
	end
end)

function kick(id)
	for i = 1, #players, 1 do
		if players[i].id == id then
			table.remove(players, i)
			DropPlayer(id, "lag switch?")
			break
		end
	end
end

RegisterServerEvent('heartbeat:Server')
AddEventHandler('heartbeat:Server', function(id)
	for i = 1, #players, 1 do
		if players[i].id == id then
			if players[i].callback == false then
				players[i].callback = true
			end
		end
	end
end)

Client:

RegisterNetEvent('heartbeat:Client')
AddEventHandler('heartbeat:Client', function(id)
    TriggerServerEvent('heartbeat:Server', id)
end)

you can see they equal the same number. Maybe its somehow a code issue, but it seems like a fivem thing. Could be wrong tho.

Some things i tried were tonumber(source), declaring it in a variable along with the players[i].id in a variable. All yielded the same result!

I suggest you use type to check if you are comparing a number to a number, or a number to a string.

In Lua, the number 3 does not equal the string “3”, so 3 == "3" is false.

type(3) == "number" and type("3") == "string"

If you check the types, you’ll discover the problem.

BTW, running tonumber() on a number has no side-effects, so it’s not a problem to accidentally turn a number into a number that way.

odd so the v in GetPlayers() returns string?

I’ve already told you how to check that. I can’t recall the type of every variable in FiveM off the top of my head, so I’d have to check. Easier to just have you check it yourself.

Yep i went and looked and for some reason it does return a string, that is very odd. Thanks for the help tho :slight_smile: