Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
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
0
There is an easier way to do this. emervise 123 — 3y
0
How? @BRO_ITmeYO TestUser4646 1 — 3y
0
I'm answering right now emervise 123 — 3y
0
Thx TestUser4646 1 — 3y

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?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 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.

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.

0
At the top what goes inside 1, 2, and 3 for local pads? TestUser4646 1 — 3y
Ad
Log in to vote
0
Answered by
emervise 123
3 years ago

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
0
Make something like this for every pad. emervise 123 — 3y
0
Note: If you use this make sure you make the Vote1, 2, and 3 a value. emervise 123 — 3y
0
I'm confused about this. So for each pad do I have to make a script like this but change the pad numbers? TestUser4646 1 — 3y
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.. emervise 123 — 3y
View all comments (2 more)
0
Never mind I can't lol emervise 123 — 3y
0
Why doesn't the script make an IntValue??? TestUser4646 1 — 3y