← All guides
Guide 5

Build your first steal-and-defend game

Every player gets a base. Grab the gem from the middle, run it home to score, and tag anyone who carries it.

Free70 minutes · Beginner, last guide · Roblox Studio
1
Step 1 / 8

Set up the place

  1. Open Roblox Studio, click New Experience and choose the Baseplate template.
  2. The gem will sit in the middle of the map. Four bases will stand around it, each about 60 studs from the middle.
  3. This game needs at least two players, so at the end you will test it with two test players.

More detail

  • The Baseplate template is in the list of templates on the start screen.
  • The Position field in Properties shows where a part is. Position 60, 0.5, 0 is 60 studs from the middle of the map.
  • Move the SpawnLocation, the platform where players appear, close to the middle, so every player starts at the same distance from the gem.
  • On a Mac, use the Cmd key every time this guide says Ctrl.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

2
Step 2 / 8

Build the bases

  1. In Explorer hover over Workspace, click + and add a Folder. Name it Bases.
  2. Add a Part. Set Size to 16, 1, 16, turn on Anchored and make it grey. Drag it into Bases and name it Base1.
  3. Duplicate it three times with Ctrl+D. Name the copies Base2, Base3 and Base4 and move one to each side of the map.

More detail

  • To put the part into the folder, drag its name in Explorer onto the name Bases.
  • Copies made with Ctrl+D are already inside the Bases folder. Rename each one with F2.
  • The names must be exactly Base1, Base2, Base3 and Base4, with no space before the number.
  • A server with more than four players has more players than bases. Later you can set the server size to 4 in the game settings on Creator Hub.
Tip. Keep all four bases the same distance from the middle. Then the game is fair for everyone.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

3
Step 3 / 8

Create the gem

  1. Add a Part in the middle of the map, about 4 studs above the floor. Set Size to 2, 2, 2, pick a bright color and set Material to Neon. Name it Gem.
  2. Turn on Anchored and Massless, and turn off CanCollide.
  3. Hover over Gem, click + and add a ProximityPrompt. Set ActionText to Take and HoldDuration to 1.

What each part does

  • A ProximityPrompt shows a button when a player stands close. Holding it for 1 second triggers it.
  • Massless on and CanCollide off keep the gem from slowing down or pushing the player who carries it.

More detail

  • Anchored, Massless and CanCollide are checkboxes in Properties.
  • When you click +, type ProximityPrompt to find it. Click it, then change ActionText and HoldDuration in Properties.
  • In the game, a small button with the letter E and the word Take appears when you stand near the gem.
  • On a phone, players hold their finger on that button.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

4
Step 4 / 8

Give every player a base and a score

  1. Hover over ServerScriptService, click + and add a Script. Name it Game.
  2. Delete the line that is already there and paste this code. It is the first of three parts.
  3. Press Play. One base turns green. That one is yours.
ServerScriptService > Game (1 / 3)
local Players = game:GetService("Players")

local bases = workspace:WaitForChild("Bases")
local gem = workspace:WaitForChild("Gem")
local home = gem.CFrame
local carrier = nil

Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player

	local score = Instance.new("IntValue")
	score.Name = "Score"
	score.Parent = stats

	for _, base in ipairs(bases:GetChildren()) do
		if base:GetAttribute("Owner") == nil then
			base:SetAttribute("Owner", player.UserId)
			base.Color = Color3.fromRGB(60, 220, 120)
			break
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	for _, base in ipairs(bases:GetChildren()) do
		if base:GetAttribute("Owner") == player.UserId then
			base:SetAttribute("Owner", nil)
			base.Color = Color3.fromRGB(160, 160, 160)
		end
	end
end)

What each part does

  • The first lines find the bases and the gem and remember where the gem started.
  • SetAttribute writes the owner's UserId onto the first free base.
  • PlayerRemoving frees the base again when its owner leaves.

More detail

  • The script opens in a new tab. Click inside the code, press Ctrl+A and then Ctrl+V to replace everything.
  • Which base turns green depends on the order of the bases in the folder. Walk around to find yours.
  • The player list in the top right corner shows Score 0.
  • Nothing happens yet when you walk to the gem and hold E. That comes in the next step.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

5
Step 5 / 8

Let players take the gem

  1. Open the Game script again.
  2. Paste this code below the code from the last step.
  3. Press Play, walk to the gem and hold E. The gem jumps above your head and follows you.
ServerScriptService > Game (2 / 3)
local function resetGem()
	carrier = nil

	local weld = gem:FindFirstChild("CarryWeld")
	if weld then
		weld:Destroy()
	end

	gem.Anchored = true
	gem.CFrame = home
	gem.ProximityPrompt.Enabled = true
end

gem.ProximityPrompt.Triggered:Connect(function(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if carrier or not root then return end

	carrier = player
	gem.ProximityPrompt.Enabled = false
	gem.Anchored = false
	gem.CFrame = root.CFrame * CFrame.new(0, 4, 0)

	local weld = Instance.new("WeldConstraint")
	weld.Name = "CarryWeld"
	weld.Part0 = root
	weld.Part1 = gem
	weld.Parent = gem
end)

What each part does

  • carrier remembers who holds the gem. Nobody else can take it while it is carried.
  • A WeldConstraint glues the gem to the player, so it moves with them.
  • resetGem removes the weld and puts the gem back in the middle.

More detail

  • To open the script again, double-click Game in Explorer.
  • Click after the very last character of the code and press Enter twice. Then press Ctrl+V. Do not press Ctrl+A this time, or you replace the first part.
  • The gem floats 4 studs above your head while you carry it.
  • To test: take the gem, then check that the Take button is gone for as long as you carry it.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

6
Step 6 / 8

Add scoring and tagging

  1. Paste this last part at the very end of the Game script.
  2. Press Play, take the gem and run onto your green base. Score goes up by 1 and the gem returns to the middle.
ServerScriptService > Game (3 / 3)
for _, base in ipairs(bases:GetChildren()) do
	base.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player and player == carrier and base:GetAttribute("Owner") == player.UserId then
			player.leaderstats.Score.Value += 1
			resetGem()
		end
	end)
end

while true do
	task.wait(0.2)

	if carrier then
		local weld = gem:FindFirstChild("CarryWeld")
		local root = weld and weld.Part0
		local character = root and root.Parent
		local humanoid = character and character:FindFirstChildOfClass("Humanoid")

		if not humanoid or humanoid.Health <= 0 or root.Position.Y < -20 then
			resetGem()
		else
			for _, other in ipairs(Players:GetPlayers()) do
				local otherRoot = other ~= carrier and other.Character and other.Character:FindFirstChild("HumanoidRootPart")
				if otherRoot and (otherRoot.Position - root.Position).Magnitude < 5 then
					resetGem()
					break
				end
			end
		end
	end
end

What each part does

  • Each base checks three things: a player touched it, that player carries the gem, and the base belongs to them.
  • The loop at the bottom runs 5 times a second. If another player comes closer than 5 studs to the carrier, the gem goes back.
  • The loop also resets the gem if the carrier leaves the game or falls off the map.

More detail

  • Again click after the very last character of the script, press Enter twice and paste.
  • The finished script has three parts in this order: bases and score, taking the gem, scoring and tagging.
  • A grey base that belongs to nobody gives no points. Only your own green base counts.
  • Alone you cannot test tagging. You do that with two players in the next step.
Common mistake. Pasting this part in the middle of the script. The while loop never ends, so any code below it never runs.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

7
Step 7 / 8

Test with two players

  1. Next to the Play button open the small menu and choose Server & Clients. Set the number of players to 2 and press Play.
  2. Studio opens two game windows. Each one is a different player with a different base.
  3. Take the gem in one window. In the other window run into that player. The gem returns to the middle.
  4. Stop the test with the red Stop button in the main Studio window, then switch the menu back to Test.

More detail

  • The small menu is the arrow right next to the Play button at the top of Studio.
  • The two game windows open on top of each other. Drag one aside so you can see both.
  • Click inside a window before you use the keyboard. The keys go to the window you clicked last.
  • Testing with two windows needs a computer with enough memory. If it is very slow, close other programs first.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

8
Step 8 / 8

Publish it

  1. Choose File → Publish to Roblox. Give the game a name and a short description.
  2. On the Creator Hub website open your game and fill in the Maturity questionnaire.
  3. Set the game to Public and send the link to a friend.

More detail

  • The first time you publish, Studio asks for a name and a description. Click Create.
  • Creator Hub is the website create.roblox.com. Sign in with the same account and click your game in the list.
  • Roblox may ask for more before a game can be public, for example an age check on the account. The page tells you what is missing.
  • If you are under 18, do this step together with a parent.
Tip. This game is only fun with other players. Invite two or three friends to the same server for the first match.
This step does not work for me

Check these three things first

  • The name of every part and script is exactly as in the guide, with the same capital letters.
  • The script sits inside the right object. Look at the label above the code box.
  • Open the Output window from the Window menu. Red text shows the script and the line with the problem.

Still stuck? Write to us →

Now change it yourself

  • End the round when a player reaches 5 points and set every score back to 0.
  • Add a second gem with a different color.
  • Make the carrier slower. Hint: set Humanoid.WalkSpeed to 12 when the gem is taken and back to 16 in resetGem.