local Selection = script.Parent local Pad1 = Selection.Pad1 local Pad2 = Selection.Pad2 local Pad3 = Selection.Pad3 Pad1Value = 0 local CheckTheCheck = false local valueChanged = false local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) local voted = Instance.new("BoolValue") voted.Name = "VoteCheck" voted.Parent = player while true do if CheckTheCheck == true then if voted.Value == false then wait() valueChanged = true voted = true end end wait() end end) while true do Pad1.Touched:Connect(function() CheckTheCheck = true wait() if valueChanged == true then Pad1Value = Pad1Value + 1 valueChanged = false print(Pad1Value) end end) Pad2.Touched:Connect(function() end) Pad3.Touched:Connect(function() end) wait(1) end
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.
local Selection = script.Parent local Pads = { [1] = {}, [2] = {}, [3] = {}, } function removeVoteFromPad( padNumber, votingPlayer ) --Removes the votingPlayer from Pads[padNumber] ONLY IF their vote is counted on that pad local padVoteIndex = table.find(Pads[padNumber], votingPlayer) if padVoteIndex then table.remove(Pads[padNumber], padVoteIndex) end end function addVoteToPad( padNumber, votingPlayer ) --Adds the votingPlayer to Pads[padNumber], removing them from any other pads if applicable removeVoteFromPad(padNumber, votingPlayer) table.insert(Pads[padNumber], votingPlayer) end function onPadTouched( padNumber, touchingPart ) local touchingCharacter = touchingPart.Parent local touchingPlayer = game.Players:GetPlayerFromCharacter(touchingCharacter) if touchingPlayer then addVoteToPad(padNumber, touchingPlayer) end end function onPadTouchEnded( padNumber, touchingPart ) local touchingCharacter = touchingPart.Parent local touchingPlayer = game.Players:GetPlayerFromCharacter(touchingCharacter) if touchingPlayer then removeVoteFromPad(padNumber, touchingPlayer) end end function countPadVotes( padNumber ) return #Pads[padNumber] end Pad1.Touched:Connect(function( touchingPart ) onPadTouched( 1, touchingPart ) end) Pad2.Touched:Connect(function( touchingPart ) onPadTouched( 2, touchingPart ) end) Pad3.Touched:Connect(function( touchingPart ) onPadTouched( 3, touchingPart ) end) Pad1.TouchEnded:Connect(function( touchingPart ) onPadTouchEnded( 1, touchingPart ) end) Pad2.TouchEnded:Connect(function( touchingPart ) onPadTouchEnded( 2, touchingPart ) end) Pad3.TouchEnded:Connect(function( touchingPart ) onPadTouchEnded( 3, touchingPart ) end)
EDIT: Some things that aren't quite right with your solution:
while true do Pad1.Touched:Connect(function() end) wait(1) end
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.
local voted = Instance.new("BoolValue") voted.Name = "VoteCheck" voted.Parent = player
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.
Put this in ServerScriptService
local Pad1 = game.Workspace.Pad1 local Pad2 = game.Workspace.Pad2 local Pad3 = game.Workspace.Pad3 local Votes1 = game.Workspace.Votes1 local Votes2 = game.Workspace.Votes2 local Votes3 = game.Workspace.Votes3 Pad1.Touched:Connect(function(player) if player:FindFirstChild("IntValue") then if player.IntValue == Pad2 then Votes2.Value = Votes2.Value - 1 Votes1.Value = Votes1.Value + 1 elseif player.IntValue == Pad3 then Votes3.value = Votes3.Value - 1 Votes1.value = Votes1.Value + 1 else local IntValue = instance.new("IntValue") IntValue.Parent = player
Closed as Non-Descriptive by matiss112233, JesseSong, EmbeddedHorror, and Nguyenlegiahung
This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.
Why was this question closed?