Better way to make an election system

I’m currently making an election game that allows the player to simply, vote.

How I’m currently doing this is whenever they press the vote button, it will send a webhook to a Discord channel and I’ll manually count it by using the search features.

Another idea is that I’ll save the number to a Datastore and every time they vote it will +1 of the current number in the datastore then send a webhook of the current number, but I don’t know if it will work accurately or not.

Is there any other way to make it more efficient?

That’s probably the better way to go if you want them to vote in roblox.
Would suggest looking into using Ordered Datastores for that, so you can get a list of the top voted players

1 Like

If 2 people voted at almost the same time, would it in any way affect the accuracy of all the votes?

1 Like

Realistically, yes, specifically if there is more than one server. However, you’d probably never hit limitations if you limit an individual to one singular vote (and try to reduce vote spam)

1 Like

Assuming that roblox datastores don’t go derp and that you have proper error checking, it should not be a problem. (And if anything will be added to the que)

1 Like

The server would all be 1 player server.

I would probably disable the button right when they clicked it to avoid as said, spamming.

1 Like

If you’re using a singular player server, and there happens to be multiple instances then UpdateAsync (function of the DataStore class) would probably be the most reasonable option. Because you’ll also need to know when other servers update said values.

1 Like

UpdateAsync works with a normal database right? Because it says on the api reference “GlobalDataStores”

1 Like

Normal roblox datastore, here’s a link to reference GlobalDataStore | Roblox Creator Documentation

1 Like

I’ll try and write one to make sure it’s right:

local datastore = game:GetService("DataStoreService"):GetDataStore("Person1")

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, voted)
  if game.Players:FindFirstChild(player.Name) then
    local success, votes = pcall(function()
      return datastore:GetAsync("Votes")
    end)
    local votes = votes or 0
    local success, err = pcall(function()
      datastore:UpdateAsync("Votes", tonumber(votes)+1)
    end)
  end 
end)

So the error in this one is that UpdateAsync takes a function as the second param.
So it’d look more like

datastore:UpdateAsync("Votes", function(oldValue)
    return oldValue + 1;
end)
1 Like
local datastore = game:GetService("DataStoreService"):GetDataStore("Person1")

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, voted)
    local success, err = pcall(function()
      datastore:UpdateAsync("Votes", function(oldValue)
      local newValue = oldValue or 0
      newValue += 1
      return newValue
    end)
  end 
end)

Would this be correct?

That’d be the more correct way to do so, how many people do you intend on having in your vote system?

1 Like

It would be anywhere from 3-5 people.
Should I make it into one datastore instead of a few?

I see, I’d recommend flopping some things around if that’s alright.

local VoteStore = game:GetService("DataStoreService"):GetDataStore("Votes");

-- differentiate based on key

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, votedFor)
    local success, err = pcall(function()
      datastore:UpdateAsync(votedFor, function(oldValue)
      local newValue = oldValue or 0
      newValue += 1
      return newValue
    end)
  end 
end)

something similar to this however probably wrote better cus exploiters would just be able to fire the remote with said arguments and spam the votes.

Being only one player is in the server, and you only get one vote you could, not the best way to handle it but’d it get the job done as long as no one joins that server after the player leaves

local Con;

Con = game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, votedFor)
    if Con then Con:Disconnect() end
1 Like

What I was thinking is that I add a :Kick() at the end of the remote event and then save them to a datastore which prevents them from joining again, so even if the exploiter did exploit, it would just be the same as voting normally.

Eh I wouldn’t necessarily recommend kicking, you could just use a table and store if the player has voted, but to each their own kicking would work too.


local alreadyVoted = {};

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, votedFor)
    if not alreadyVoted[player] then
        alreadyVoted[player] = true;
        -- rest of code here
    end
end)
1 Like

Wouldn’t that reset when the server shuts down?

Alas that’s true, but so does kicking, unless you intend on banning the user.

1 Like

Well I did add this

save them to a datastore which prevents them from joining again