Points
Documentation

Writing Luau

Write automations as code when blocks aren't enough. How a script is structured, worked examples, and what the sandbox allows.

Every automation is Luau underneath. Luau is the scripting language Roblox uses, a fast and friendly version of Lua. Switch the editor to Code to read or write it directly. The editor checks your code as you type, autocompletes everything Points provides, and points to errors by line number.

You don't have to pick one or the other. Code you write by hand shows up in the block view as a Custom code block, and everything around it stays editable as blocks.

How a Script Is Structured

A script declares its inputs and triggers at the top level, and puts the work inside the trigger's function:

points.input.integer("allowance", "Points each member gets per day", { default = 10, min = 1 })

points.schedule("daily-allowance", { cron = "0 0 * * *", timezone = "America/New_York" }, function(ctx)
	local result = points.members():add(points.inputs.allowance)
	points.log(`Gave {result.count} members their allowance.`)
end)

When you save, Points runs the top level of the script once to find out which inputs, schedules and commands it has. That's how it knows to register commands and when to run schedules. Calls that read or change data, like points.getMember or points.send, aren't available at that point, so keep the top level to declarations and put everything else inside functions.

When a trigger fires, the script runs from the top again and then calls that trigger's function.

Example: a Lottery With a Growing Jackpot

Members pay to play. Every ticket goes into a jackpot kept in storage, and a winner takes the whole thing.

points.input.integer("price", "Ticket price", { default = 10, min = 1 })
points.input.number("chance", "Chance to win, in percent", { default = 2, min = 0.1, max = 100 })

points.command({
	name = "lottery",
	description = "Buy a ticket for the jackpot",
	cooldown = 60,
}, function(ctx)
	local price = points.inputs.price

	if ctx.member.balance < price then
		ctx:reply(`A ticket costs {price} points.`, { ephemeral = true })
		return
	end

	ctx.member:subtract(price)
	local jackpot = points.storage.increment("jackpot", price)

	if math.random() * 100 < points.inputs.chance then
		ctx.member:add(jackpot)
		points.storage.set("jackpot", 0)
		ctx:reply(`<@{ctx.member.id}> won the {jackpot} point jackpot!`)
	else
		ctx:reply(`No luck. The jackpot is now {jackpot} points.`, { ephemeral = true })
	end
end)

Example: Weekly Top Three

Every Monday, post the three richest members and pay each of them a bonus from the bank.

points.input.channel("channel", "Where to post the top three")
points.input.integer("bonus", "Bonus for each of the top three", { default = 250, min = 1 })

points.schedule("weekly-top", { cron = "0 12 * * 1", timezone = "Europe/London" }, function(ctx)
	local top = points.members():list(3)
	local fields = {}

	for place, member in top do
		table.insert(fields, {
			name = `#{place}`,
			value = `<@{member.id}> with {member.balance} points`,
		})
		points.bank.pay(member, points.inputs.bonus)
	end

	points.send(points.inputs.channel, {
		title = "This week's top three",
		description = `Each gets a {points.inputs.bonus} point bonus.`,
		color = 0x0695F1,
		fields = fields,
	})
end)

list returns members sorted by balance, highest first, so list(3) is the top three.

Example: Paying a Role That Members Pick

A command with options, where a moderator picks the role and amount each time.

points.command({
	name = "payrole",
	description = "Pay everyone with a role from the bank",
	permission = "moderator",
	options = {
		points.option.role("role", "Who to pay"),
		points.option.integer("amount", "How much each", { min = 1, max = 5000 }),
	},
}, function(ctx)
	local members = points.members({ role = ctx.options.role })
	local cost = members:count() * ctx.options.amount

	if points.bank.balance() < cost then
		ctx:reply(`That would cost {cost} points, more than the bank has.`, { ephemeral = true })
		return
	end

	members:each(function(member)
		points.bank.pay(member, ctx.options.amount)
	end)

	ctx:reply(`Paid {ctx.options.amount} points to everyone with <@&{ctx.options.role}>.`)
end)

Failing on Purpose

Call error("message") to stop a run. Nothing it changed is kept, and the message is shown to the member for commands or recorded in the run history for schedules.

if ctx.options.amount > points.bank.balance() then
	error("The bank can't cover that")
end

Use print(...) to write to the run's output while you debug. It shows up in test results and in the run history.

What the Sandbox Allows

Scripts run in a sandbox. They can only see and change the server they're installed in.

  • Standard Luau is available, including string, table, math (with math.random), os.time and os.date, and string interpolation with backticks.
  • require, loadstring, getfenv, setfenv, coroutine and collectgarbage are removed.
  • Scripts can't make web requests or read files.
  • Nothing carries over between runs except points.storage.

See Luau Reference for everything Points adds, and Limits for how much a single run can do.

Need help?