Skip to content

Instantly share code, notes, and snippets.

@jacobhumston
Last active April 30, 2023 06:13
Show Gist options
  • Save jacobhumston/081a13ad0f3ad1fea4162a8c2f3f4df0 to your computer and use it in GitHub Desktop.
Save jacobhumston/081a13ad0f3ad1fea4162a8c2f3f4df0 to your computer and use it in GitHub Desktop.

Types

type PayloadResponse = { 
        Success: number, 
        Message: string?, 
        Payload: any? 
}

type VoteResults = { 
	Red: {
		Count: number?,
		Players: {number}
	}, 
	Green: {
		Count: number?,
		Players: {number}
	} 
}

Remote function and remote event:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VotingRemoteFunction = ReplicatedStorage.VotingRemoteFunction
local VotingRemoteEvent = ReplicatedStorage.VotingRemoteEvent

You don't really need the remote event, but it can be used if you need it.

VotingRemoteEvent

The remote event only has one event at the moment, and that is whenever the current vote ends. Please note that this will only be emitted to the player that caused the vote to start in the first place.

An example:

local VotingRemoteEvent = ReplicatedStorage:WaitForChild("VotingRemoteEvent")
VoitingRemoteEvent.OnClientEvent:Connect(function(Event)
  if Event == "VoteEnded" then
    -- whatever
  end
end)

VotingRemoteFunction

This allows you to call all of the supported methods of the backend system like starting votes, etc.

An example:

local VotingRemoteFunction = ReplicatedStorage:WaitForChild("VotingRemoteFunction")
local Result = VotingRemoteFunction:InvokeServer('event', payload)
if Result.Success then
  local Payload = Result.Payload
  -- whatever
else
  error(Result.Message)
end

Event is obviously the name of the event, payload is the event's parameter. All methods return a PayloadResponse, the fields are self explanatory.

VotingRemoteFunction Methods

--// *Name*           -> Payload Response | Expected Payload           | Method Description                    |
--// ------------------------------------ | -------------------------- | ------------------------------------- |
--// StartVote        -> RemoteEvent      | Payload: { Time: number? } | Start a vote.                         |
--// EndVote          -> nil              | Payload: nil               | End the current running vote.         |
--// GetResults       -> VoteResults      | Payload: nil               | Get current vote results.             |
--// ClearResult      -> nil              | Payload: nil               | Clear current vote results.           |
--// GetIsVoteRunning -> boolean          | Payload: nil               | Check if a vote is currently running. | 

Here is an example when starting a vote:

local Remote : RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild("VotingRemoteFunction")
print(Remote:InvokeServer("StartVote", { Time = 2 * 60 }))

Notes:

  • If no time is specified, you will need to call EndVote in order for the vote to end. If you don't want to pass a time, pass time as nil or just pass an empty table.
  • Anyone in studio can use the system, in-game, only dyna can.
  • Clear results, and get results can be executed at any time, even if a vote is ongoing/not happening.
  • Vote results are only cleared when a vote is started or when ClearResults is called.
  • You can use the RemoteEvent returned by the StartVote to know when the vote is over. It's the same instance as VotingRemoteEvent.

Let me know if you have any questions. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment