Answered by
4 years ago Edited 4 years ago
There's a lot wrong with your script, so instead of changing it to work I'll post my own solution to the problem.
Here's how I would do it: instead of just keeping track of the number of players who voted for each pad, keep track of which players voted for which pads. We can do this with one table for each pad. This lets us do some sanity checks whenever we add or remove a vote to a pad, e.g. making sure each player only votes on one pad.
01 | local Selection = script.Parent |
08 | function removeVoteFromPad( padNumber, votingPlayer ) |
11 | local padVoteIndex = table.find(Pads [ padNumber ] , votingPlayer) |
13 | table.remove(Pads [ padNumber ] , padVoteIndex) |
17 | function addVoteToPad( padNumber, votingPlayer ) |
20 | removeVoteFromPad(padNumber, votingPlayer) |
22 | table.insert(Pads [ padNumber ] , votingPlayer) |
25 | function onPadTouched( padNumber, touchingPart ) |
26 | local touchingCharacter = touchingPart.Parent |
27 | local touchingPlayer = game.Players:GetPlayerFromCharacter(touchingCharacter) |
28 | if touchingPlayer then |
29 | addVoteToPad(padNumber, touchingPlayer) |
33 | function onPadTouchEnded( padNumber, touchingPart ) |
34 | local touchingCharacter = touchingPart.Parent |
35 | local touchingPlayer = game.Players:GetPlayerFromCharacter(touchingCharacter) |
36 | if touchingPlayer then |
37 | removeVoteFromPad(padNumber, touchingPlayer) |
41 | function countPadVotes( padNumber ) |
42 | return #Pads [ padNumber ] |
45 | Pad 1. Touched:Connect( function ( touchingPart ) |
46 | onPadTouched( 1 , touchingPart ) |
49 | Pad 2. Touched:Connect( function ( touchingPart ) |
50 | onPadTouched( 2 , touchingPart ) |
53 | Pad 3. Touched:Connect( function ( touchingPart ) |
54 | onPadTouched( 3 , touchingPart ) |
57 | Pad 1. TouchEnded:Connect( function ( touchingPart ) |
58 | onPadTouchEnded( 1 , touchingPart ) |
61 | Pad 2. TouchEnded:Connect( function ( touchingPart ) |
62 | onPadTouchEnded( 2 , touchingPart ) |
65 | Pad 3. TouchEnded:Connect( function ( touchingPart ) |
66 | onPadTouchEnded( 3 , touchingPart ) |
EDIT: Some things that aren't quite right with your solution:
2 | Pad 1. Touched:Connect( function () |
Setting up a connection makes that connection exist as long as the relevant object exists (in this example Pad1) or until it's manually disconnected. Making .Touched connections in a while loop like this means that a new connection gets set up every second, taking up more and more memory and computation. It will also almost certainly break your game, because whatever happens in the connected function will happen e.g. 100 times after 100 seconds instead of just once like you intended. You just need to set up the connection once, it will still call the listener function every time Pad1 is touched.
1 | local voted = Instance.new( "BoolValue" ) |
2 | voted.Name = "VoteCheck" |
Putting a BoolValue inside the player object is fine, but keep in mind that any changes made to it (like setting voted.Value to true or false) will only work in one direction, from the server to the client. If a server script changes it, it will be visible to both the server and the client. If the player changes it e.g. in a LocalScript, it will not be visible to the server and the server won't know that the player has cast a vote.