Menu
CoddyTech

The Defense Script

Lesson 1 of 22 in Coddy's Roblox Game: Tower Defense course.

In this course you build a tower defense game: enemies march along a path in waves, and you stop them with towers you buy, place, upgrade and sell before they reach your base.

The game has two scripts. DefenseServer, a Script, runs on Roblox's server and owns every rule: the enemies, the money, the base. DefenseClient, a LocalScript inside the HUD, runs on each player's device and draws their screen. Its file is a note until chapter 2.

Every number that balances the game is in one ModuleScript, ReplicatedStorage.Settings, so you can tune the whole game from one place:

local Settings = require(game.ReplicatedStorage.Settings)
print(Settings.BaseHealth) --> 20
challenge icon

Challenge

Easy

Start the game's server Script, DefenseServer.

  1. Load the settings: local Settings = require(game.ReplicatedStorage.Settings).
  2. Print "The base can take " .. Settings.BaseHealth .. " hits".
  3. When a player joins, give them a leaderstats Folder holding an IntValue named Money that starts at Settings.StartMoney.

Press Play: the Output shows The base can take 20 hits, and the leaderboard shows Money 200. Then Check.

Try it yourself

-- DefenseServer: the Script that runs the game, on the server.
local Players = game:GetService("Players")

-- 1. Load the settings: require the ModuleScript
--    game.ReplicatedStorage.Settings


-- 2. Print how many hits the base can take, using Settings.BaseHealth


-- 3. Give every player Money on the leaderboard, starting at
--    Settings.StartMoney (a Folder named leaderstats holding an IntValue)

All lessons in Roblox Game: Tower Defense

Practice on your own: Roblox playground