Value seems to only be added to on every second call

im currently working on a votekick, but for some reason it only adds to the vote value every second vote

			for index = 1, #KickDictionary do
				local TempVal = tostring(KickDictionary[index])
				print (TempVal)
				if TempVal == message[2] then
					Votes = Votes + 1
				end
			end

im really out of options here as to why it would only add to every second time it is called…

This is the whole script
Banlist = {}

KickDictionary = {}

function Clean(player, message)
	if message:sub(1,9):lower() == "votekick/" then
		message = message:split("/")
		

		local Votes = 0
		
		
		local VotedAlready = false
		local ValidPlayer = false
		
		for _, instance in ipairs(game.Players:GetPlayers()) do
			if message[2] == instance.Name then
				ValidPlayer = true
			end
		end
		
		if ValidPlayer == true then
			for index = 1, #KickDictionary do
				if not KickDictionary[index] then
					if player.Name == KickDictionary[index] then
						if message[2] == KickDictionary[player.Name] then
						print(player.Name.." already voted to kick "..message[2])
						VotedAlready = true
						
						elseif message[2] ~= KickDictionary[player.Name] then 
						KickDictionary[player.Name] = message[2]
						
						print(player.Name.." voted to kick "..message[2])
						
						print(KickDictionary[index])
						end
					end
				end
			end
			if VotedAlready == false then	
			KickDictionary[#KickDictionary + 1] = player.Name
			KickDictionary[player.Name] = message[2] 
			
			print(unpack(KickDictionary))
			
			end
			

			for index = 1, #KickDictionary do
				local TempVal = tostring(KickDictionary[index])
				print (TempVal)
				if TempVal == message[2] then
					Votes = Votes + 1
				end
			end


			if Votes >= MinVotes then
				local TempVal = #Banlist + 1
				Banlist[TempVal] = message[2]
				local Plr = game.Players:WaitForChild(tostring(message[2]))
				Plr:kick("you are kicked")
			end
			
			print(Votes, MinVotes)

			
		elseif ValidPlayer == false then
			print(player.Name.." tried kicking "..message[2].." which isnt here")
		end
	end
end

function AddMinVotes(player)
	for index = 1, #Banlist do
		if Banlist[index] == player.Name then
			player:Kick("Youre kicked")
		end
	end

	MinVotes = 0
	for _, instance in ipairs (game.Players:GetPlayers()) do
		MinVotes = MinVotes + .5
	end

	player.Chatted:Connect(function (message) 
		Clean(player, message) 
	end)
end

function SubtractMinVotes(player)
	for index = 1, #KickDictionary do
		if KickDictionary[index] == player.Name then
			table.remove(KickDictionary, index)
		end
	end
	MinVotes = 0
	for _, instance in ipairs (game.Players:GetPlayers()) do
		MinVotes = MinVotes + .5
	end
end

game.Players.PlayerAdded:Connect(AddMinVotes)
game.Players.PlayerRemoving:Connect(SubtractMinVotes)

I tried to look through your code, but I got confused so I just wrote my own version of what you’re looking for.

Should work in theory but, not going to lie, I haven’t tested it.

I left an excessive amount of comments aswell to try to make it easier to read.

Hope this helps!

-- vote to kick


voteableToKick = {}


-- returns Player when given part of player's name
function findPlayer(str)
	for i,v in pairs(game.Players:GetPlayers()) do
		if string.find(v.Name,str) then
			return v
		end
	end
end

-- count how many votes a player has against him/her
function countPlayerVotes(player)
	local count = 0
	for i,v in pairs(voteableToKick[player.Name]) do
		if v == true then
			count = count + 1
		end
	end
	return count
end

-- removes all votes by said player
function removeAllVotesByPlayer(player)
	for i,v in pairs(voteableToKick) do
		for i2,v2 in pairs(v) do
			if i2 == player.Name then
				voteableToKick[i][i2] = false
			end
		end
	end
end

-- call removeAllVotesByPlayer whenever a player is leaving
game.Players.PlayerRemoving:connect(removeAllVotesByPlayer)

-- for every player added
function addPlayer(player)

	-- if the player already has a bunch of votes against them, kick them
	if countPlayerVotes(player)> math.floor(#game.Players:GetPlayers()/2)  then
		targetPlayer:Kick('sorry but you are still vote kicked :T')
	end

	-- add the player to the voteableToKick list
	voteableToKick[targetPlayer.Name] = {}

	-- whenever the player chats
	player.Chatted:connect(function(msg)

		-- split the message into a table of all the words
		local splitMsg = msg:split("/")
	
		-- if the first word is votekick
		if splitMsg[1] == "votekick" then

			-- find the player
			local targetPlayer = findPlayer(splitMsg[2])

			-- if a player is found
			if targetPlayer then
				-- make the record show that player does not like targetPlayer
				voteableToKick[targetPlayer.Name][player.Name] = true

				-- if more than half the server's player count has voted to kick targetPlayer
				if countPlayerVotes(targetPlayer)> math.floor(#game.Players:GetPlayers()/2)  then
					-- kick that boi
					targetPlayer:Kick('u were vote kicked >:O')
				end
				
			end
			
		end
	end)
end

-- call addPlayer whenever a player joins
game.Players.PlayerAdded:connect(addPlayer)

-- call addPlayer to everyone on the server (just incase)
for i,v in pairs(game.Players:GetPlayers()) do
	addPlayer(v)
end

im terribly sorry if my code is chaotic, i actually got lost in it a few times myself

1 Like