Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Practice Challenges: UI, Data, Remotes and Security

Test your Roblox skills with twelve challenges on UI maths, events, DataStore safety, remotes and security, each with a runnable JavaScript model and a read-only Luau solution.

You learn to script by writing scripts, not by reading about them. This lesson gives you twelve challenges that mirror real problems from the second half of the course. Each challenge has a short description, a runnable JavaScript model so you can test the logic without Studio, and the Luau version to type into a script when you try it in Roblox Studio.

Try each one yourself first. Write your answer in the editor, run it, and only then read the solution. The Luau snippets are read-only because the Roblox engine does not run on this site.

Part 1: UI and events

Challenge 1: A centred panel

Compute the pixel size and top-left position of a panel that is 40 percent of the screen width, 50 percent of the height, centred with an anchor point of (0.5, 0.5), on a 1000 by 600 screen.

JS Challenge 1: centred panel

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Solution idea: width = 0.4 * 1000 = 400 and height = 0.5 * 600 = 300. The panel's centre sits at (500, 300), so left = 500 - 200 = 300 and top = 300 - 150 = 150. In Luau:

LUA
panel.Size = UDim2.fromScale(0.4, 0.5)
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.Position = UDim2.fromScale(0.5, 0.5)

Challenge 2: A once-only button

Write a function makeOnce(fn) that returns a function that runs fn only on the first call, like Signal:Once. Compare with your answer in the Luau solution below.

JS Challenge 2: once

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
button.Activated:Once(function()
	print("clicked!")
end)

-- or by hand:
local used = false
button.Activated:Connect(function()
	if used then return end
	used = true
	print("clicked!")
end)

Challenge 3: Debounce a touch

A part fires Touched many times per second. Make it award a point at most once every 3 seconds per player. The model below uses a fake clock, so decide which timestamps get a point.

JS Challenge 3: per-player cooldown

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local last = {}
part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end
	local now = os.clock()
	if now - (last[player] or -math.huge) < 3 then return end
	last[player] = now
	player.leaderstats.Points.Value += 1
end)

Part 2: Data and services

Challenge 4: Merge defaults

Write reconcile(saved, defaults) so a table missing keys receives the defaults, but values the player already has stay unchanged. Test with { coins: 7 } and defaults { coins: 0, gems: 0, level: 1 }.

JS Challenge 4: reconcile

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local function reconcile(saved, defaults)
	for key, value in pairs(defaults) do
		if saved[key] == nil then
			saved[key] = value
		end
	end
	return saved
end

Challenge 5: Retry with backoff

Write an async retry(fn, attempts) that calls fn, and on error waits 1, 2, 4 ... time units (just count them here) before trying again. Return [true, result] or [false, lastError].

JS Challenge 5: retry

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local function retry(callback, attempts)
	local lastError
	for i = 1, attempts do
		local ok, result = pcall(callback)
		if ok then return true, result end
		lastError = result
		task.wait(2 ^ (i - 1))
	end
	return false, lastError
end

Challenge 6: Lost update

Two servers each add coins to a shared record: +100 and +10 starting from 50. Using an update function that takes the current value (like UpdateAsync) show that the total ends at 160, not 60 or 150.

JS Challenge 6: update function

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
store:UpdateAsync(key, function(old)
	old = old or { coins = 0 }
	old.coins += 100
	return old
end)

Part 3: Remotes and security

Challenge 7: Validate a number

Write isValidAmount(n) that is true only for whole numbers from 1 to 50, and false for strings, NaN, infinity, fractions and negatives.

JS Challenge 7: validator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local function isValidAmount(n)
	return typeof(n) == "number" and n == n and n >= 1 and n <= 50 and n % 1 == 0
end

Challenge 8: Range check

A player requests to open a chest. Accept only if the player is within 10 studs of it (use a distance formula on x and z) and the chest is not already open.

JS Challenge 8: distance and state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local function openChest(player, chest)
	local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
	if not root then return "no character" end
	local flat = Vector3.new(chest.Position.X - root.Position.X, 0, chest.Position.Z - root.Position.Z)
	if flat.Magnitude > 10 then return "too far" end
	if chest:GetAttribute("Open") then return "already open" end
	chest:SetAttribute("Open", true)
	return "opened"
end

Challenge 9: Dispatch table

Implement a handler that accepts actions "Wave", "Sit" and "Dance" only, and refuses everything else, using an object lookup instead of a long if chain.

JS Challenge 9: dispatch

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local actions = {
	Wave = function(player) return player.Name .. " waves" end,
	Sit = function(player) return player.Name .. " sits" end,
	Dance = function(player) return player.Name .. " dances" end,
}

local function handle(player, action)
	local fn = typeof(action) == "string" and actions[action]
	if not fn then return "refused" end
	return fn(player)
end

Part 4: Publishing and money

Challenge 10: Idempotent receipts

Write process(receipt) that grants coins once per PurchaseId even if the same receipt arrives twice, returning "granted" both times.

JS Challenge 10: receipts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

LUA
local processed = {} -- saved with the player's data in real life
MarketplaceService.ProcessReceipt = function(info)
	if processed[info.PurchaseId] then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	-- grant here...
	processed[info.PurchaseId] = true
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

Challenge 11: Day-1 retention

Given a list of players with join day and visit days, compute day-1 retention as a percentage.

JS Challenge 11: retention

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Challenge 12: Launch checklist

Extend the checklist idea from Publishing: add two checks of your own (for example "has a privacy policy link" or "tested on a phone"), and print which items fail.

JS Challenge 12: your own checklist

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Answers to the unsolved models

For Challenges 1 to 12 the Luau code above shows the pattern. Here are the JavaScript answers for the models, so you can compare.

JS Model answers (selected)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What makes Challenge 6's update safe from lost updates?

    Applying the change to the latest value (as UpdateAsync does) means both additions accumulate.
  2. 2. In Challenge 9, why is a lookup table safer than a long if chain?

    A table of allowed handlers makes the allowed set explicit; unknown names are refused.
  3. 3. What does "idempotent" mean for receipts?

    Roblox may call ProcessReceipt more than once for the same purchase, so the handler must not reward twice.

Summary

  • Practise by writing logic first in a plain model, then translating it into Luau for Studio.
  • UI maths uses scale, offset and anchors; events need debounces and Once.
  • Data code needs defaults, retries, and UpdateAsync-style updates.
  • Remote handlers validate shape, range and permission, and use dispatch tables.
  • Purchases must be idempotent, and analytics such as retention are simple arithmetic.

Next: Back to the Roblox Studio and Luau course