Example mods
Complete mods to paste into Settings > Mods and run. The test suite compiles every one of them, so they stay valid as the language changes.
- Arena builder Chat commands,
Map.AddBlock, a per-player undo stack - Damage rules A
Damagehook, atask.waitloop,Units.InRadius,Units.Died - King of the Hill
RegisterGamemode,SetAutoEnd(false)+EndMatch, scoring per second - Scoreboard UI panels,
UI.Format, buttons, a text box, vote to end - Spectator queue Spectators,
Player:SetTeam,Teams.Eliminated, a"Spectators"panel audience
Arena builder
Chat commands that edit the map during a match.
Shows Chat commands, Map.AddBlock, a per-player undo stack
--[[
Arena builder: chat commands that edit the map during a match.
!wall build a 12-stud wall in front of you
!tower build a tall pillar where you stand
!undo remove your last block
!clear remove every block this mod built
]]
local placed: { [number]: { any } } = {} -- UserId -> stack of blocks
local function push(player, block)
local stack = placed[player.UserId] or {}
placed[player.UserId] = stack
table.insert(stack, block)
end
local commands = {}
function commands.wall(player)
local pos = player.Position
if not pos then
return
end
push(
player,
Map.AddBlock({
Position = pos + Vector3.new(0, 2, -8),
Size = Vector3.new(12, 6, 1),
Material = "Brick",
Color = Color3.fromRGB(150, 80, 60),
})
)
end
function commands.tower(player)
local pos = player.Position
if not pos then
return
end
push(
player,
Map.AddBlock({
Position = pos + Vector3.new(0, 8, 0),
Size = Vector3.new(4, 20, 4),
Material = "Concrete",
})
)
end
function commands.undo(player)
local stack = placed[player.UserId]
local block = stack and table.remove(stack)
if block then
block:Destroy()
end
end
function commands.clear()
Map.ClearBlocks()
table.clear(placed)
end
Players.Chatted:Connect(function(player, message)
local name = string.match(message, "^!(%a+)$")
local command = name and commands[string.lower(name)]
if not command then
return
end
if not Game.IsRunning() then
player:Notify("Start a match first")
return
end
local ok, err = pcall(command, player)
if not ok then
player:Notify("Could not build: " .. tostring(err))
end
end)
print("Arena builder ready: !wall, !tower, !undo, !clear")
Damage rules
A Damage hook plus a periodic heal aura. Hooks run synchronously inside the game's damage code: keep them short.
Shows A Damage hook, a task.wait loop, Units.InRadius, Units.Died
--[[
Damage rules: a Damage hook plus a periodic heal aura.
Hooks run synchronously inside the game's damage code: keep them short.
]]
local ARMORED = { ["Heavy Trooper"] = true, Tank = true }
Game.SetHook("Damage", function(target, source, amount)
if ARMORED[target.Type] then
amount *= 0.5 -- armored units take half damage
end
if source and source.Type == "Sniper" then
amount *= 1.5
end
return amount
end)
-- Command Centers heal nearby friendly units every 2 seconds.
task.spawn(function()
while true do
task.wait(2)
if Game.IsRunning() then
for _, cc in Buildings.GetAll({ Type = "Command Center" }) do
for _, unit in Units.InRadius(cc.Position, 20, { Team = cc.Team }) do
unit:Heal(5)
end
end
end
end
end)
Units.Died:Connect(function(unit)
if unit.SpawnedByMod then
return
end
local team = unit.Team
if team and team.Exists then
team:AddMoney(2) -- small refund for every lost unit
end
end)
King of the Hill
Hold the glowing hill in the middle of the map. Each second, the team with the most units on the hill scores a point. First to TARGET wins. Command Centers still matter: no auto-end on CC loss.
Shows RegisterGamemode, SetAutoEnd(false) + EndMatch, scoring per second
--[[
King of the Hill: hold the glowing hill in the middle of the map.
Each second, the team with the most units on the hill scores a point.
First to TARGET wins. Command Centers still matter: no auto-end on CC loss.
]]
local TARGET = 120
local HILL_RADIUS = 18
Game.RegisterGamemode("King of the Hill", { CanAlly = false, DamageMultiplier = 1.5 })
local function mapCenter(): Vector3
local sum, count = Vector3.zero, 0
for _, team in Teams.GetTeams() do
local spawn = team.SpawnPosition
if spawn then
sum += spawn
count += 1
end
end
return if count > 0 then sum / count else Vector3.zero
end
local function playRound()
Game.SetAutoEnd(false)
local center = mapCenter()
local hill = Map.AddBlock({
Position = center - Vector3.new(0, 0.4, 0),
Size = Vector3.new(HILL_RADIUS * 2, 0.6, HILL_RADIUS * 2),
Color = Color3.fromRGB(255, 225, 0),
Material = "Neon",
Transparency = 0.6,
Collide = false,
})
local scores: { [string]: number } = {}
Game.Announce(`King of the Hill! Hold the center. First to {TARGET} points wins.`)
while Game.IsRunning() do
task.wait(1)
local counts: { [string]: number } = {}
for _, unit in Units.InRadius(center, HILL_RADIUS) do
counts[unit.TeamName] = (counts[unit.TeamName] or 0) + 1
end
local leader, best, tied = nil, 0, false
for team, n in counts do
if n > best then
leader, best, tied = team, n, false
elseif n == best then
tied = true
end
end
if leader and not tied then
scores[leader] = (scores[leader] or 0) + 1
hill.Color = Teams.Get(leader).Color
if scores[leader] % 30 == 0 then
Game.Announce(`{leader} holds the hill: {scores[leader]}/{TARGET}`)
end
if scores[leader] >= TARGET then
Game.Announce(`{leader} is King of the Hill!`)
Game.EndMatch({ leader })
break
end
end
end
end
Game.MatchStarted:Connect(function()
if Game.GetGamemode() == "King of the Hill" then
playRound()
end
end)
-- Make the next match use this mode (the gamemode is available a moment after registering).
task.delay(2, function()
local maps = Game.GetMaps()
if #maps > 0 then
Game.SetNextMatch(maps[1], "King of the Hill")
print("Next match: King of the Hill on " .. maps[1])
end
end)
Scoreboard
A panel with kills per team, a match timer, a vote to end the match and a message board, using the UI API.
Shows UI panels, UI.Format, buttons, a text box, vote to end
--[[
Scoreboard: a panel with kills per team, a match timer, a vote to end the match
and a message board, using the UI API.
* UI.Format keeps the timer and scores cheap: the template is filtered once,
the numbers are filled in without another filter call.
* Text typed into the TextBox arrives as a PlayerText (already filtered as the
player's own text); showing it uses the filtered version.
]]
local VOTE_SHARE = 0.6 -- share of players needed to end the match
local panel = UI.Create("Panel", { Title = "Scoreboard", Anchor = "TopRight", Width = 260 })
local timer = panel:Add("Text", { Text = "Waiting for a match", Size = "Caption", Color = "TextMuted" })
local scores = panel:Add("Panel") -- nested panel: one line per team
local voteBar = panel:Add("ProgressBar", { Progress = 0, Label = "Vote to end", Color = "Danger" })
local buttons = panel:Add("Row")
local voteButton = buttons:Add("Button", { Text = "Vote end", Style = "Danger" })
local hideButton = buttons:Add("Button", { Text = "Hide board" })
local board = panel:Add("Text", { Text = "", Size = "Caption" })
local input = panel:Add("TextBox", { Placeholder = "Say something to everyone", MaxLength = 80 })
local kills: { [string]: number } = {}
local lines: { [string]: any } = {} -- team name -> Text widget
local votes: { [number]: boolean } = {}
local function refreshScores()
for _, team in Teams.GetTeams() do
local line = lines[team.Name]
if not line then
line = scores:Add("Text", { Color = team.Color })
lines[team.Name] = line
end
line.Text = UI.Format("{1}: {2:int} kills", team, kills[team.Name] or 0)
end
end
local function refreshVotes()
local count = 0
for _ in votes do
count += 1
end
local needed = math.max(1, math.ceil(#Players.GetPlayers() * VOTE_SHARE))
voteBar.Progress = count / needed
voteBar.Label = UI.Format("Vote to end: {1}/{2}", count, needed)
if count >= needed and Game.IsRunning() then
Game.Announce("The match was ended by vote.")
Game.EndMatch()
end
end
Units.Died:Connect(function(unit)
-- credit the team that did not lose the unit; good enough for a two-team match
for _, team in Teams.GetTeams() do
if team.Name ~= unit.TeamName then
kills[team.Name] = (kills[team.Name] or 0) + 1
end
end
refreshScores()
end)
voteButton.Clicked:Connect(function(player)
votes[player.UserId] = if votes[player.UserId] then nil else true
refreshVotes()
end)
hideButton.Clicked:Connect(function()
board.Visible = not board.Visible
hideButton.Text = if board.Visible then "Hide board" else "Show board"
end)
input.Submitted:Connect(function(player, text)
board.Text = UI.Format("{1}: {2}", player, text)
end)
Game.MatchStarted:Connect(function()
table.clear(kills)
table.clear(votes)
scores:Clear()
table.clear(lines)
refreshScores()
refreshVotes()
end)
Players.PlayerRemoving:Connect(function(player)
votes[player.UserId] = nil
refreshVotes()
end)
while true do
if Game.IsRunning() then
timer.Text = UI.Format("Match time {1:time}", Game.GetElapsed())
end
task.wait(1)
end
Spectator queue
Servers hold more players than teams, so spectators can queue for a seat.
Shows Spectators, Player:SetTeam, Teams.Eliminated, a "Spectators" panel audience
--[[
Spectator queue: servers hold more players than teams, so spectators can queue for a seat.
* spectators press JOIN QUEUE on their panel (or type !queue); the panel shows their place
* whenever a team has no player (someone left, or a player gave up with !spectate), the
first player in the queue takes it over
* an eliminated team's player goes back to spectating and to the end of the queue
Uses: Players.GetSpectators / TeamChanged / Player:SetTeam, Teams.Eliminated, and a UI panel
whose audience is "Spectators", so it follows players as they join and leave teams.
]]
local queue: { number } = {} -- UserIds, first = next
local function position(player): number?
return table.find(queue, player.UserId)
end
local function leaveQueue(player)
local at = position(player)
if at then
table.remove(queue, at)
end
end
local panel = UI.Create("Panel", { Title = "Spectating", Anchor = "Bottom", Width = 280 }, "Spectators")
local status = panel:Add("Text", { Text = "Join the queue to take over a free team.", Size = "Caption" })
local joinButton = panel:Add("Button", { Text = "Join queue", Style = "Primary" })
local function refresh()
status.Text = UI.Format("Players waiting: {1}", #queue)
end
-- the next queued spectator takes every team nobody plays
local function fillTeams()
for _, team in Teams.GetTeams() do
if team.Alive and team.Player == nil then
while #queue > 0 do
local player = Players.Get(table.remove(queue, 1))
if player and player.IsSpectator then
local ok = pcall(player.SetTeam, player, team)
if ok then
player:Notify("You now play " .. team.Name .. ".")
break
end
end
end
end
end
refresh()
end
local function join(player)
if not player.IsSpectator or position(player) then
return
end
table.insert(queue, player.UserId)
player:Notify("You joined the queue.")
fillTeams()
end
joinButton.Clicked:Connect(join)
Players.Chatted:Connect(function(player, message)
if message == "!queue" then
join(player)
elseif message == "!spectate" and not player.IsSpectator and Game.IsRunning() then
player:SetTeam(nil)
fillTeams()
end
end)
Players.TeamChanged:Connect(function(player, team)
if team then
leaveQueue(player)
end
refresh()
end)
Players.PlayerRemoving:Connect(function(player)
leaveQueue(player)
task.defer(fillTeams) -- their team is free now
end)
Teams.Eliminated:Connect(function(team)
local player = team.Player
if player then
player:SetTeam(nil)
table.insert(queue, player.UserId)
player:Notify("Your team was eliminated. You are back in the queue.")
end
refresh()
end)
Game.MatchEnded:Connect(function()
table.clear(queue)
refresh()
end)
refresh()