Menu
CoddyTech

Training Dummy

Lesson 5 of 25 in Coddy's Roblox Game: Battlegrounds course.

The dummy needs two things to be a training dummy: a health bar, and to come back when it is beaten.

Its HealthBar is a BillboardGui with a green Fill inside a dark Back. UDim2.fromScale(health / MaxHealth, 1) makes the fill as wide as the health left.

When its Humanoid Died, remember where it stood (GetPivot), remove it, wait Settings.DummyRespawn seconds and put a fresh copy from ServerStorage in its place. The fresh one needs the same setup, so the setup is a function that calls itself for the new dummy:

local function setupDummy(dummy)
	-- health bar, and on Died: ... setupDummy(fresh)
end
setupDummy(workspace.Dummy)
challenge icon

Challenge

Easy

Give the dummy a health bar and bring it back when beaten.

  1. Write setupDummy(dummy): on HealthChanged, size dummy.HealthBar.Back.Fill to the health left.
  2. On Died: keep dummy:GetPivot(), wait 0.5 seconds, destroy the dummy.
  3. Then wait Settings.DummyRespawn, clone ServerStorage.Dummy to that pivot in workspace, and call setupDummy on it.
  4. Call setupDummy(workspace.Dummy).

Press Play and beat the dummy: its bar empties, and it comes back. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local lastSwing = {} -- player -> when they last swung

-- Roblox's own sword animations
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- A swing: the animation, then everyone in the box in front of you is hit
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- the box in front of you, never your own body
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- each humanoid once per swing
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and not hit[humanoid] then
			hit[humanoid] = true
			humanoid:TakeDamage(Settings.SwordDamage)
			print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
		end
	end
end

-- Every player gets a sword every life
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

-- Lesson 5: Training Dummy goes here

All lessons in Roblox Game: Battlegrounds

Practice on your own: Roblox playground