Help with global voting system

Hello, dev forum.

I’ve recently been having some issues creating a global voting system. I originally was thinking to use Roblox databases to store info, then I would ban the player from joining/voting again.

I can’t seem to nail it, and I would like some assistants.

I’ve looked at numerous articles, on the dev forum and hub, and I can’t find anything related to it.

Example:

game.Players.PlayerAdded:Connect(function(player)
	local Voted = Instance.new("BoolValue", player)
	Voted.Name = "Voted"
	local VotedFor = Instance.new("StringValue", Voted)
	VotedFor.Name = "Voted4"
	local ScreenGui = script.ScreenGui:Clone()
	local vote1 = ScreenGui.Vote1
	local vote2 = ScreenGui.Vote2
	ScreenGui.Parent = player:WaitForChild("PlayerGui")
	local votestore = Datastore:GetDataStore("VoteStore")
	local votedforstore = Datastore:GetDataStore("VotedForStore")
	
	vote1.MouseButton1Click:Connect(function()
		Voted.Value = true
		VotedFor.Value = "yes"
		votestore:SetAsync(player.UserId, Voted.Value)
		votedforstore:SetAsync(player.UserId, VotedFor.Value)
	end)
	
	vote2.MouseButton1Click:Connect(function()
		Voted.Value = true
		VotedFor.Value = "no"
		votestore:SetAsync(player.UserId, Voted.Value)
		votedforstore:SetAsync(player.UserId, VotedFor.Value)
	end)
end)

Can I have some tips? I want to save the “voted” and “votedfor” value.

2 Likes

i cant really help, but I just wanted to point out:

hold up!!! why are you putting the MouseButton1Click event in a SERVER SCRIPT? that’s so whacky!! don’t do that!!

2 Likes

Oh wow, I didn’t catch that one.

I’m going to fix that right now, thanks for the feedback.

Lol

What you can do instead, is reference the ScreenGui inside StarterGui with a LocalScript instead as I don’t believe you’re able to replicate its Events on the server side with GUI Objects

You’ll also need to fire RemoteEvents if you want DataStores to save on the server:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game.Players.PlayerAdded:Connect(function(player)
	local Voted = Instance.new("BoolValue", player)
	Voted.Name = "Voted"
	local VotedFor = Instance.new("StringValue", Voted)
	VotedFor.Name = "Voted4"
end)

Event.OnServerEvent:Connect(function(Player, VoteType)
    if VoteType == "yes" then
		Voted.Value = true
		VotedFor.Value = "yes"
		votestore:SetAsync(Player.UserId, Voted.Value)
		votedforstore:SetAsync(Player.UserId, VotedFor.Value)
    elseif VoteType == "no" then
		Voted.Value = true
		VotedFor.Value = "no"
		votestore:SetAsync(Player.UserId, Voted.Value)
		votedforstore:SetAsync(Player.UserId, VotedFor.Value)
    end
end)
--This should be a LocalScript parented inside both of your Vote UI's
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local VoteType = --You determine what type it is; Yes or No
local Button = script.Parent

Button.MouseButton1Down:Connect(function()
    Event:FireServer(VoteType)
end)
1 Like

I was actually working the exact same thing, just in a much different format.

This looks a lot more crisp, I’ll take a deeper dive into this.

It works!

Mine is a lot more messy, but it functions as intended. Now all I need is a way to save the values.

Local script:

local Event2 = game.ReplicatedStorage:WaitForChild("Voted2")
local VoteType1 = "no"
local VoteType2 = "yes"

script.Parent.Vote1.MouseButton1Down:Connect(function()
	Event:FireServer(VoteType1)
end)

script.Parent.Vote2.MouseButton1Click:Connect(function()
	Event2:FireServer(VoteType2)
end)

Server Script:

local Datastore = game:GetService("DataStoreService")
local Replicated = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
	local Voted = Instance.new("BoolValue", player)
	Voted.Name = "Voted"
	local VotedFor = Instance.new("StringValue", Voted)
	VotedFor.Name = "Voted4"
	local ScreenGui = script.ScreenGui:Clone()
	local vote1 = ScreenGui.Vote1
	local vote2 = ScreenGui.Vote2
	ScreenGui.Parent = player:WaitForChild("PlayerGui")
	local votestore = Datastore:GetDataStore("VoteStore")
	local votedforstore = Datastore:GetDataStore("VotedForStore")
	local eve1 = Replicated:FindFirstChild("Voted1")
	local eve2 = Replicated:FindFirstChild("Voted2")
	eve1.OnServerEvent:Connect(function(VoteType1)
		Voted.Value = true
		VotedFor.Value = "yes"
		votestore:SetAsync(player.UserId, Voted.Value)
		votedforstore:SetAsync(player.UserId, VotedFor.Value)
	end)
	eve2.OnServerEvent:Connect(function(VoteType2)
		Voted.Value = true
		VotedFor.Value = "no"
		votestore:SetAsync(player.UserId, Voted.Value)
		votedforstore:SetAsync(player.UserId, VotedFor.Value)
	end)
end)

That would work as well, but I’d just put the ScreenGui variable inside the StarterGui Service in your Server Script if it’s already being cloned & parented to all Player’s GUI when they first join the game

A bit more organized:

local Datastore = game:GetService("DataStoreService")
local Replicated = game:GetService("ReplicatedStorage")

local votestore = Datastore:GetDataStore("VoteStore")
local votedforstore = Datastore:GetDataStore("VotedForStore")

local eve1 = Replicated:WaitForChild("Voted1")
local eve2 = Replicated:WaitForChild("Voted2")

game.Players.PlayerAdded:Connect(function(player)
	local Voted = Instance.new("BoolValue", player)
	Voted.Name = "Voted"
	local VotedFor = Instance.new("StringValue", Voted)
	VotedFor.Name = "Voted4"
end)

eve1.OnServerEvent:Connect(function(player, VoteType1)
	player.Voted.Value = true
	player.Voted4.Value = "yes"
	votestore:SetAsync(player.UserId, Voted.Value)
	votedforstore:SetAsync(player.UserId, VotedFor.Value)
end)

eve2.OnServerEvent:Connect(function(player, VoteType2)
	player.Voted.Value = true
	player.Voted4.Value = "no"
	votestore:SetAsync(player.UserId, Voted.Value)
	votedforstore:SetAsync(player.UserId, VotedFor.Value)
end)

Another side note is that the Player is already passed in as the first parameter when you call OnServerEvent, so you don’t have to encase it inside your PlayerAdded event :wink: