Table.find() returns nil

Hello, developers

I’m currently trying to build a map voting system for my game
I’m trying to remove a vote when a player leaves but table.find() returns nil even if the object exists

The code is in a ModuleScript

local module = {}

module.forestVoters = {}
module.canyonVoters = {}

-- other code here

function module.removeVoter(username)
	print(module.forestVoters, module.canyonVoters)
	if table.find(module.forestVoters, username) then
		table.remove(module.forestVoters, table.find(module.forestVoters, username))
		script.Parent.ForestPad.pad.votes.Value -= 1 -- this is the value that points to how many votes the map has
		
	elseif table.find(module.canyonVoters, username) then
		table.remove(module.canyonVoters, table.find(module.canyonVoters, username))
		script.Parent.CanyonPad.pad.votes.Value -= 1
		
	else
		warn("cannot find user")
	end
end

-- other code here

return module

and this is the code that runs the function

local handler = require(script.Parent.voteHandler)

game.Players.PlayerRemoving:Connect(function(username)
	handler.removeVoter(username)
end)

in the output it shows that my username is in the table but it can’t find it inside the table
image

If you have any questions let me know

Thanks

PlayerRemoving returns the Player Instance not the Name of the Player, replace:

game.Players.PlayerRemoving:Connect(function(username)
	handler.removeVoter(username)
end)

With:

game.Players.PlayerRemoving:Connect(function(username)
	handler.removeVoter(username.Name)
end)
2 Likes

Oh. my. god. What a silly mistake, thank you so much for pointing it out!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.