The Fight Script
Lesson 1 of 25 in Coddy's Roblox Game: Battlegrounds course.
In this course you build a battlegrounds game: a sword, abilities on keys and phone buttons, combos and blocks, and bots that hunt you around the arena.
The game has two scripts. BattleServer, a Script, runs on Roblox's server and owns every rule. BattleClient, a LocalScript in the HUD, runs on each player's device; it holds only a comment until chapter 2. Every number that balances the game is in ReplicatedStorage.Settings.
A sword is a Tool. A player's tools live in their Backpack, and the Backpack is emptied every time they respawn, so the sword is handed out on each CharacterAdded, not once when they join:
player.CharacterAdded:Connect(function()
local sword = ServerStorage.Sword:Clone()
sword.Parent = player.Backpack
end)Press 1 (or tap the sword at the bottom of the screen) to hold it.
Challenge
EasyStart the fight script, BattleServer, and hand out a sword.
- Load the settings:
local Settings = require(game.ReplicatedStorage.Settings). - Print
"Swords deal " .. Settings.SwordDamage .. " damage". - In
Players.PlayerAdded, on everyplayer.CharacterAdded, cloneServerStorage.Swordintoplayer.Backpack.
Press Play and press 1: the Output reads Swords deal 10 damage and you hold a sword. Then Check.
Try it yourself
-- BattleServer: the Script that runs the game, on the server.
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
-- 1. Load the settings: require the ModuleScript
-- game.ReplicatedStorage.Settings
-- 2. Print how much a sword hit does, using Settings.SwordDamage
-- 3. When a player joins: every time their character spawns, put a
-- copy of ServerStorage.Sword in their Backpack
All lessons in Roblox Game: Battlegrounds
Practice on your own: Roblox playground