Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make this voting system work so each player has their own vote?

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

4 comments

0

There is an easier way to do this.

0

How? @BRO_ITmeYO

0

I'm answering right now

0

Thx

2 answers

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.

1 comment

0

At the top what goes inside 1, 2, and 3 for local pads?

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

6 comments

0

Make something like this for every pad.

0

Note: If you use this make sure you make the Vote1, 2, and 3 a value.

0

I'm confused about this. So for each pad do I have to make a script like this but change the pad numbers?

0

No. In the same script type that for each pad but you'll have to tweak the code for each section. Actually I'll just do another answer..

0

Never mind I can't lol

0

Why doesn't the script make an IntValue???

This discussion is preserved as it was. New answers, comments, and votes are closed.