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 4 years ago
01local Selection = script.Parent
02local Pad1 = Selection.Pad1
03local Pad2 = Selection.Pad2
04local Pad3 = Selection.Pad3
05 
06Pad1Value = 0
07 
08local CheckTheCheck = false
09local valueChanged = false
10 
11local Players = game:GetService("Players")
12Players.PlayerAdded:Connect(function(player)
13    local voted = Instance.new("BoolValue")
14    voted.Name = "VoteCheck"
15    voted.Parent = player
View all 45 lines...
0
There is an easier way to do this. emervise 123 — 4y
0
How? @BRO_ITmeYO TestUser4646 1 — 4y
0
I'm answering right now emervise 123 — 4y
0
Thx TestUser4646 1 — 4y

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

01local Selection = script.Parent
02local Pads = {
03    [1] = {},
04    [2] = {},
05    [3] = {},
06}
07 
08function removeVoteFromPad( padNumber, votingPlayer )
09    --Removes the votingPlayer from Pads[padNumber] ONLY IF their vote is counted on that pad
10 
11    local padVoteIndex = table.find(Pads[padNumber], votingPlayer)
12    if padVoteIndex then
13        table.remove(Pads[padNumber], padVoteIndex)
14    end
15end
View all 67 lines...

EDIT: Some things that aren't quite right with your solution:

1while true do
2    Pad1.Touched:Connect(function()
3 
4    end)
5    wait(1)
6end

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.

1local voted = Instance.new("BoolValue")
2voted.Name = "VoteCheck"
3voted.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 — 4y
Ad
Log in to vote
0
Answered by
emervise 123
4 years ago

Put this in ServerScriptService

01local Pad1 = game.Workspace.Pad1
02local Pad2 = game.Workspace.Pad2
03local Pad3 = game.Workspace.Pad3
04local Votes1 = game.Workspace.Votes1
05local Votes2 = game.Workspace.Votes2
06local Votes3 = game.Workspace.Votes3
07 
08Pad1.Touched:Connect(function(player)
09    if player:FindFirstChild("IntValue") then
10        if player.IntValue == Pad2 then
11            Votes2.Value = Votes2.Value - 1
12            Votes1.Value = Votes1.Value + 1
13        elseif player.IntValue == Pad3 then
14            Votes3.value = Votes3.Value - 1
15            Votes1.value = Votes1.Value + 1
16    else
17        local IntValue = instance.new("IntValue")
18        IntValue.Parent = player
0
Make something like this for every pad. emervise 123 — 4y
0
Note: If you use this make sure you make the Vote1, 2, and 3 a value. emervise 123 — 4y
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 — 4y
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 — 4y
View all comments (2 more)
0
Never mind I can't lol emervise 123 — 4y
0
Why doesn't the script make an IntValue??? TestUser4646 1 — 4y