Code issues - Voting system

Sure i can do that
local votes = script.Parent.Count.Value

How can I make each player vote by pressing the button? let it be your personal vote

Yes - you’d have to do this locally with Local Scripts. It is not a good idea to add server scripts into Guis, or use server scripts to modify GUIs.

Do you think you can help me with that? Please, I’ve been looking for a solution for weeks … :frowning:

I did it from a local and it didnt worked

Sure thing - this one time I’ll do the heavy lifting, however please in future try to engineer it yourself!

Firstly - put the server script in ServerScriptService.
Then, put the Con1 RemoteEvent in ReplicatedStorage. Call it “VoteEvent”

Now:

LocalScript Code:

-- Click on vote 1
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')
local GlobalVoteCounter = ReplicatedStorage:WaitForChild('VoteCounter')

local VoteButton = script.Parent:WaitForChild('Vote1')
local VoteCount = script.Parent:WaitForChild('VoteCount1')
VoteCount.Text = "The vote number is: " .. tostring(GlobalVoteCounter.Value)

VoteButton.MouseButton1Click:Connect(function()
	VoteEvent:FireServer()
end)

GlobalVoteCounter.Changed:Connect(function(Value)
	VoteCount.Text = "The vote number is: " .. tostring(Value)
end)

ServerScript Code:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')
local GlobalVoteCounter = Instance.new("IntValue")
GlobalVoteCounter.Name = "VoteCounter"
GlobalVoteCounter.Parent = ReplicatedStorage

VoteEvent.OnServerEvent:Connect(function(Player)

	if GlobalVoteCounter.Value == 0 then
		GlobalVoteCounter.Value += 1
		print("votes = 0 -> "..GlobalVoteCounter.Value)
	end

end)
1 Like

Thank you very much but it is not working at all well, the other player cannot vote :frowning:

Yes - you added it in that way in your original code. Remove the if statement and it’ll function fine.

localscript:

-- Click on vote 1

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')

local GlobalVoteCounter = ReplicatedStorage:WaitForChild('VoteCounter')

local VoteButton = script.Parent:WaitForChild('Vote1')

local VoteCount = script.Parent:WaitForChild('VoteCount1')

VoteCount.Text = "The vote number is: " .. tostring(GlobalVoteCounter.Value)

VoteButton.MouseButton1Click:Connect(function()

VoteEvent:FireServer()

end)

GlobalVoteCounter.Changed:Connect(function(Value)

VoteCount.Text = "The vote number is: " .. tostring(Value)

end)

serverscript:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')
local GlobalVoteCounter = Instance.new("IntValue")
GlobalVoteCounter.Name = "VoteCounter"
GlobalVoteCounter.Parent = ReplicatedStorage

VoteEvent.OnServerEvent:Connect(function(Player)
	
	if GlobalVoteCounter.Value == 0 then
		GlobalVoteCounter.Value += 1
		print("votes = 0 -> "..GlobalVoteCounter.Value)
	end

end)

image

still not working :frowning: what’s wrong? :frowning:

As I said - you didn’t remove the if statement…

this? i did it and it doesn’t work

Remove the if statement that’s checking if the votes are zero.

2 Likes

is it good? or i’m wrong? :frowning:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')
local GlobalVoteCounter = Instance.new("IntValue")
GlobalVoteCounter.Name = "VoteCounter"
GlobalVoteCounter.Parent = ReplicatedStorage

VoteEvent.OnServerEvent:Connect(function(Player)
	
	GlobalVoteCounter.Value = 1
	
	if GlobalVoteCounter.Value == 0 then
		GlobalVoteCounter.Value += 1
		print("votes = 0 -> "..GlobalVoteCounter.Value)
	end

end)
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local VoteEvent = ReplicatedStorage:WaitForChild('VoteEvent')
local GlobalVoteCounter = Instance.new("IntValue")
GlobalVoteCounter.Name = "VoteCounter"
GlobalVoteCounter.Parent = ReplicatedStorage

VoteEvent.OnServerEvent:Connect(function(Player)
		GlobalVoteCounter.Value += 1

		print("votes = " .. GlobalVoteCounter.Value - 1 .. " -> " .. GlobalVoteCounter.Value)
	end

end)

That works makes the +1 but how do I get each player to vote only 1 time?

example,

player vote vote → +1 (1 vote)
player 2 vote → +1 (1 vote)

only 1 vote for every player

What this script does is increase +1 repeatedly for each player

You would have to make an array where you store the users that have voted and check if a user has already voted or not. If he hasn’t you let him vote. Then whenever the vote is done, clear the array.

1 Like

and what about to make a int value with a parent for each player, is it good and works?

Wouldn’t this code just keep adding to the value? What if they already voted and want to undo their vote?

The best way I could think of is adding an INT value to every player upon joining. Let’s say 0 == no vote, 1-3 == voted. In a local script containing the mouse events, you can fire a remote event TO the server with the parameter of what they voted for.

You want to check for server-side validation to make sure the vote is valid, and if it IS valid, you can toggle their vote.

Here’s a demo of what it looks like: vote.rbxl (27.5 KB)

Feel free to study how the voting system works. It’s incredibly simple.

This has a lot of flexibility since it is an actual instance value. After the voting ends, you can make ALL the player’s Vote value 0 and update the client.
It’s also not easy to exploit since it TOGGLES the client’s vote, so they can’t just spam a bunch of requests with one particular vote and expect it to keep increasing the vote count.

1 Like

Thank you very much, this worked … it is quite difficult to read the script but it will take me time to learn, thanks you a lot! :smiley:

Haha, I should’ve added more comments looking back on it, but if you need me to explain any particular line(s) of code, I would gladly help you out with that.