Player Voting Module/Custom API

Player Voting API Module

I’ve made yet another API cool for if you want to quickly making a game with little experience. This time, it’s player voting API’s with capabilities to add votes (without stacking), remove votes, clear votes, get verious results and get the highest/winner as well as signal events. (This is also being used in some of my games which is why I made it and then open sourced it)

Get the model here | Github Repo


Installation Setup:

  1. Download the module either via the creator store model or the github repo
  2. Add the module script to the game and preferably put it in Replicated Storage or Server Storage (If in server storage then only scripts in server storage or server script service can access it)
  3. Rename the module to whatever you want to call it. It’s named MainModule so that it can be required by asset ID but doing it that way may cause yielding issues
  4. To be able to use it in a script, you must simply require it like this:
local mainModule = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

In this case, I’ve named it VotingAPI and placed it in ReplicatedStorage.


Tutorial / API Guide:

Fireable Functions:

module:AddPlayerVote(Player, VoteOn : string)

Use this when you want to add a player to a voting option. Votes are non stackable and will change if voted on something else (If you don’t want that then use the module script multiple times)

Parameters:

Parameter Type Function
Player Player Instance The player instance for the vote to be applied to. You can use PlayerService if you need to get player by character.
VoteOn String The name of something that is vote able (This can also be something that isn’t listed to be voted on to create something new to vote on)

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

script.Parent.Touched:Connect(function(Collided)
   local player = game:GetService("Players"):GetPlayerByCharacter(Collided.Parent)
   if player then
      votingModule:AddPlayerVote(player, "VotingExample")
   end
end)

The following code is what would be used in a player stepped on or collided with a non collidable part (game object) with the script. This would simply add the players vote to the selected option.

module:RemovePlayerVote(Player, VoteOn : string)

RemovePlayerVote is used to remove a player’s vote from a specified voting option. This function should NOT be used if a player is changing voting option as that is done automatically anyways.

Parameters:

Parameter Type Function
Player Player Instance The player instance for the vote to be applied to. You can use PlayerService if you need to get player by character.
VoteOn String The name of something that is already a voting category and the player has already voted for

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

script.Parent.Touched:Connect(function(Collided)
   local player = game:GetService("Players"):GetPlayerByCharacter(Collided.Parent)
   if player then
      votingModule:RemovePlayerVote(player, "VotingExample")
   end
end)

The following code is what would be used in a player stepped on or collided with a non collidable part (game object) with the script. This would simply remove the player vote to the selected option as long as it’s a valid option and the player did vote for it.

module:ClearVoting()

This function will remove all votes from the voting option’s as well as any new ones created with AddPlayerVote. This should be used for when you want to create a new voting section for your game.

Parameters:
There are no parameters available for this function

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

while wait(20) do
   print(module.GetAllVotingResults())
   votingModule:ClearVoting()
end)

This code will clear the voting every 20 seconds but before that it will print out the full results.

Returnable Functions

module.GetAllVotingResults()

GetAllVotingResults is a returnable function that gets all stored information about a vote and returns it to where it was fired in the script.

Parameters:
There are no parameters available for this function

Returns:

Variable Type Function
Full Results Jagged Array/Table The full table of results from the voting in a jagged array. The first array is the voting catagories and then the second array is the UserId’s that voted for that category

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

script.Parent.Touched:Connect(function(Collided)
   print(module.GetAllVotingResults())
end)

The code example will return the jagged array version of the results to the voting including each catagory and the user id’s that voted for each catagory (Printed version may only be viewable to a script or in studio output because it’s an array)

module.GetHighestVotingResults()

GetHighestVotingResults will return the winning result/the voting option with the highest amount of votes. If there is a tie, this will still return with one option so if a tied vote is relevent to your game then use GetAllVotingResults instead.

Parameters:
There are no parameters available for this function

Returns:

Variable Type Function
Winning Result String Returns the string of the catagory that at the time of being fired has won the majority in the vote.

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

script.Parent.Touched:Connect(function(Collided)
   print(module.GetAllVotingResults())
end)
module.GetMapVotingResults(OptionName : string)

This function will return a specified map’s voting results at the time of being fired returning a table/array of user id’s that voted for the selected option.

Parameters:

Parameter Type Function
OptionName String The string of the voting option you need the results of

Returns:

Variable Type Function
Results Array/Table Returns all UserId’s that voted for the spesified category

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

game:GetService("RunService").Heartbeat:Connect(Function()
   print(module.GetMapVotingResults("ExampleOption")) -- Returns all UserId's that voted for the option
   print(#module.GetMapVotingResults("ExampleOption")) -- Returns the number of votes for that option
end)

This script will constently print the user id’s of players that voted for the specified option and then it will print out the number of votes in that option and will do this for every frame due to it being in heartbeat.

Connectable Events:

module.OnVoteAdded

This is receivable for when a vote is added to any option

Parameters:
There are no parameters available for this function

Returns:

Variable Type Function
Option String Returns the voting option that the vote was added to
Player Player Instance Returns the player instance that voted for the option

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

module.OnVoteAdded:Connect(Function(Option : string, Player : Player)
   print(Player.." voted for "..Option)
end)
module.OnVoteRemoved

This is receivable for when a vote is removed to any option

Parameters:
There are no parameters available for this function

Returns:

Variable Type Function
Option String Returns the voting option that the vote was added to
Player Player Instance Returns the player instance that voted for the option

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

module.OnVoteRemoved:Connect(Function(Option : string, Player : Player)
   print(Player.." removed their vote for "..Option)
end)
module.OnVotesCleared

This is receivable for when all votes are cleared

Parameters:
There are no parameters available for this function

Returns:
There are no returnable variables available for this function

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

module.OnVotesCleared:Connect(Function()
   print("The votes have been cleared")
end)
module.OnVoteChanged

This is receivable for when a vote is changed from one option to another option

Parameters:
There are no parameters available for this function

Returns:

Variable Type Function
ogOption String Returns the voting option that the vote was added to
newOption String Returns the player instance that voted for the option
Player Player Instance Returns the player instance that chaged their vote

Code Example:

local module = require(game.ReplicatedStorage.VotingAPI) -- This will very based on where you put the votingModule and what you name it

module.OnVoteChanged:Connect(Function(ogOption : string, newOption : string, Player : Player)
   print(Player.." has changed their vote from "..ogOption.." to "..newOption)
end)

Hopefully, you all find good and convenient use cases for this and let me know if there is a specific module you want me to open-source next!

6 Likes