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