Skip to content

Instantly share code, notes, and snippets.

@Validark
Created February 20, 2017 20:56
Show Gist options
  • Save Validark/ec6dee7052f0d2102ae7f08d6b7bd9bd to your computer and use it in GitHub Desktop.
Save Validark/ec6dee7052f0d2102ae7f08d6b7bd9bd to your computer and use it in GitHub Desktop.
-- @author Validark
--[[
Place this in a `Script` in `ServerScriptService`
Place Arena spawns in a model called in the workspace
Make sure the name of the Arena spawns model matches variable `ARENASPAWNS` in the Configuration below
Place the Pistol in the ServerStorage
Make sure the name of the Pistol matches the variable `GEAR_NAME` in Configuration
The main things you should be modifying are in the Configuration section
The beginning of the `EndRound` function handles game winners
Game Steps:
[1] If the player count is two or greater, two random players are teleported to two respective spawns within Model `ARENASPAWNS` in workspace
[2] ServerStorage.GEAR_NAME is cloned to each Player's backpack
[3] Players duel until one player dies
[4] Dueling players respawn
[5] After a short intermission, go back to step 1
]]
-- Configuration
local GEAR_NAME = "Pistol"
local ARENASPAWNS = "ArenaSpawns"
local INTERMISSION_TIME = 10
local WAIT_TIME_AFTER_DEATH = 1.5
-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
-- Game Data
local Gear = ServerStorage[GEAR_NAME]
local Playerlist = Players:GetPlayers()
local NumPlayers = #Playerlist
-- Extract spawn positions
ARENASPAWNS = workspace[ARENASPAWNS]
local ARENASPAWNSChildren = ARENASPAWNS:GetChildren()
local SpawnAboveVector = Vector3.new(0, 5, 0)
local Spawn1 = ARENASPAWNSChildren[1].Position + SpawnAboveVector
local Spawn2 = ARENASPAWNSChildren[2].Position + SpawnAboveVector
SpawnAboveVector, GEAR_NAME, ARENASPAWNS, ARENASPAWNSChildren = ARENASPAWNS:Destroy()
-- Round Declarations
local EndRound
local Contestant1, Contestant2
local Contestant1Won, Contestant2Won
local Connections = {}
local function Cleanup()
--- Cleanup last round's data and yield for `INTERMISSION_TIME` seconds
-- Does not run the first time
function Cleanup()
-- Disconnect all Connections
for a = 1, 2 do
Connections[a]:Disconnect()
Connections[a] = nil
end
-- Load Contestants' Characters
Contestant1:LoadCharacter()
Contestant2:LoadCharacter()
-- Clear data booleans
Contestant1Won, Contestant2Won = nil
-- Wait intermission time
print("Intermission: Waiting", INTERMISSION_TIME, "seconds")
wait(INTERMISSION_TIME)
end
end
-- Event Connection functions: Don't mess with
local function Contestant1Lost()
--- Runs upon player death
Contestant2Won = true
wait(WAIT_TIME_AFTER_DEATH)
local Winner = not Contestant1Won and Contestant2
EndRound(Winner, Contestant1)
end
local function Contestant2Lost()
--- Runs upon player death
Contestant1Won = true
wait(WAIT_TIME_AFTER_DEATH)
local Winner = not Contestant2Won and Contestant1
if Winner then -- Let other connection handle Tie-games
EndRound(Winner, Contestant1)
end
end
function EndRound(Winner, Loser)
--- Runs whenever a round ends
-- @param Player Winner The contestant that won the round
if Winner then
print(Winner.Name, "has won the round!")
print(Loser.Name, "has lost the round!")
elseif Winner == false then
-- It was a tie
print("Neither player won! It was a tie!")
end
Cleanup() -- Cleanup last round's data and wait intermission time
repeat
-- Get list of Players
Playerlist = Players:GetPlayers()
NumPlayers = #Playerlist
-- Wait until there are enough players before starting round
until NumPlayers > 1 or not wait(1)
print("Starting round")
-- Pick 2 different random numbers within range [1, NumPlayers]
Contestant1 = math.random(1, NumPlayers)
repeat
Contestant2 = math.random(1, NumPlayers)
until Contestant2 ~= Contestant1
-- Get Players from Playerlist
Contestant1 = Playerlist[Contestant1]
Contestant2 = Playerlist[Contestant2]
-- Retrieve Characters
local Character1 = Contestant1.Character or Contestant1.CharacterAdded:Wait()
local Character2 = Contestant2.Character or Contestant2.CharacterAdded:Wait()
-- Teleport Characters to Arena spawns
Character1:MoveTo(Spawn1)
Character2:MoveTo(Spawn2)
-- Clone the Gear and parent it to the Contestants' Backpacks
Gear:Clone().Parent = Contestant1:WaitForChild("Backpack")
Gear:Clone().Parent = Contestant2:WaitForChild("Backpack")
-- Wait for the Players to die
Connections[1] = Character1:WaitForChild("Humanoid").Died:Connect(Contestant1Lost)
Connections[2] = Character2:WaitForChild("Humanoid").Died:Connect(Contestant2Lost)
end
EndRound()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment