✓ Accepted answer
local voted = {}
local plr = script.Parent.Parent.Parent.Parent
script.Parent.NoBttn.MouseButton1Click:connect(function()
if not voted[plr.Name] then
voted[plr.Name] = true
Val2.Value = Val2.Value + 1
end
end)
Give this a try.
It looks like this is in a LocalScript in which case this could be implemented much more simply. However, if it is in a LocalScript, it will be recreated with the player when they respawn, allowing the player to vote again.
Here is a more robust method of counting votes and disallowing multiple votes from the same player:
Create a Script object in ServerScriptService and put this code in it:
local voted = {}
local playerVote = Instance.new("RemoteFunction")
playerVote.Name = "PlayerVote"
playerVote.OnServerInvoke = function(player, vote)
if not voted[player] then
voted[player] = true
if vote then
else
Val2.Value = Val2.Value + 1
end
end
end)
playerVote.Parent = workspace
Create a LocalScript object as a sibling to the Yes and No buttons containing this code:
local playerVote = workspace:WaitForChild("PlayerVote")
script.Parent.NoBttn.MouseButton1Click:connect(function()
playerVote:InvokeServer(false)
end)
script.Parent.YesBttn.MouseButton1Click:connect(function()
playerVote:InvokeServer(true)
end)
4 comments
It's not in a local script, it's just in a script. Should I still use this method?
Yes, use the more robust method. If you are using a Script object inside StarterGui it too will be recreated when the player respawns. I added some clarification about where to put the Script and the LocalScript.
In the script that I have to put in ServerScriptService, the end) is underlined in red so I just changed it to end. Is that OK?
It's working, thank you so much! Sorry for responding so late.