Code issues - Voting system

Hello, I need to make each player when pressing the button execute this and you can see the total number of votes in the textlabel but of the players who have pressed the button, could someone help me with that please… :frowning:

LocalScript:

-- Click on vote 1

script.Parent.Vote1.MouseButton1Click:Connect(function()

	script.Parent.Con1:FireServer()

end)

ServerScript:

script.Parent.Con1.OnServerEvent:Connect(function(player)
	
	local votes = 0
	local textvote = script.Parent.VoteCount1
	
	if votes == 0 then
		votes += 1
		print("votes = 0 -> "..votes)
		textvote.Text = "The vote number is: ".. tostring(votes)
	end
	
end)

image

I need to make each player who votes show their unit vote

Honestly, the simplest way of doing this is with an IntValue which the clients can see. So, whenever a user votes, add to that IntValue and add a “Changed” Event signal listener to the client.

In the future, Attributes will be a more powerful way of doing this. However let’s walk before you can run :slight_smile:

1 Like

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?