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
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.
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