"Player & their votes" handler issue

Hello,
So I was trying to figure out how to make a touch-based map-voting handler, using tables to manage which one of the map the player has voted. Then I randomly came up with this simple one:

Touch handler

pad1.Touched:Connect(function(hit)
	local plr = PlayersService:GetPlayerFromCharacter(hit.Parent)
	if plr then
		BindEvents.MapVote:Fire(plr, vote1)
	end
end)

pad2.Touched:Connect(function(hit)
	local plr = PlayersService:GetPlayerFromCharacter(hit.Parent)
	if plr then
		BindEvents.MapVote:Fire(plr, vote2)
	end
end)

Table manager :point_down:

local plrVoteds = {}

BindEvents.MapVote.Event:Connect(function(plr, vote)
	if plr then
		local previousVote = plrVoteds[plr]
	
		if previousVote and previousVote ~= vote then
			previousVote.Value -= 1
			vote.Value += 1
		else
			--<< idk what to put here
		end
		
		plrVoteds[plr] = vote
	end
end)

The plrVoteds table is used to store which map was voted, along with the player itself.

Now the problem here is, regardless which pad I touched first, none of the vote.Value changed. But then as I touch a different pad, it seems to operate as intended.

It might look easy to solve, but myself have absolutely no idea what is happening in my script, and that’s just my numb brain.

Feedbacks are appreciated :slightly_smiling_face:

1 Like

If I have to guess what your issue is, it’s likely because of your if previousVote check. By default, when a player first steps on a pad, their previousVote will be nil, specifying the variable by itself in an if statement checks if it’s truthy, aka if it’s not false or nil.

I think a fix would maybe be to change the if statement around. If there’s no previous vote it should just increment the vote.Value, if there is a previous vote and it’s not the same vote then it should decrement the previous vote first and then increment the new vote. If there is a previous vote and it’s the same vote then nothing should happen

Also your previousVote.Value -= 1 won’t work as intended, it’ll only decrement the variable, not the value in the table, you should do plrVoteds[plr].Value -= 1

You should make a singleton for the total votes. Instead of passing an instance through a remote event, just pass a table.

2 Likes
if not previousVote then
	votes.Value += 1
			
elseif previousVote and previousVote ~= votes then
	previousVote.Value -= 1
	votes.Value += 1
else
			
end

@EmbatTheHybrid Changed it to as what you told, and it worked like a charm! :smiley:
Nice explanation, also.

I see nothing wrong. Isn’t prevVote set to be plrVoteds[plr]?

I can’t think of doing that and how I should rearrange/organize it… :thinking:


Eitherway, I’ve stated this topic as solved. Thank you for the replies :smile: @EmbatTheHybrid @ExercitusMortem

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