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