Menu
CoddyTech

상점

Coddy의 Roblox 게임: 타워 디펜스 코스 레슨. 22개 중 7번째.

이제 플레이어 측면을 살펴봅시다. DefenseClient는 HUD 안에 있는 LocalScript입니다. HUD는 Roblox가 각 플레이어의 화면에 복사하는 ScreenGui이므로 script.Parent는 HUD입니다. HUD의 Shop 프레임에는 타워마다 TextButton이 하나씩 있으며, 버튼 이름은 타워 이름을 따릅니다.

게임의 개체가 플레이어의 기기에 도달하기 전에 LocalScript가 시작될 수 있으므로, WaitForChild를 사용해 개체를 찾습니다. 버튼의 Activated 이벤트는 버튼을 클릭하거나 탭할 때 발생합니다.

local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))
local button = script.Parent.Shop.Frost
button.Text = "Frost tower"
button.Activated:Connect(function()
	print("Frost picked")
end)
challenge icon

챌린지

중급

DefenseClient에서 상점 버튼에 가격을 표시하세요.

  1. local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))와 local hud = script.Parent를 추가하세요.
  2. hud.Shop의 각 TextButton에 대해 텍스트를 button.Name .. " $" .. Settings.Towers[button.Name].Price로 설정하세요.
  3. 버튼의 Activated 이벤트가 발생하면 이름을 local selected에 저장하세요.

‘플레이’를 누르세요. 상점에 Archer $60, Cannon $120, Frost $90이 표시됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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

local enemies = workspace.Enemies

print("The base can take " .. Settings.BaseHealth .. " hits")

-- 순서대로 정렬된 웨이포인트(이름은 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

-- workspace의 속성으로 저장된 기지의 체력: 모든 클라이언트가 읽을 수 있음
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)

-- 모든 플레이어가 리더보드에서 Money를 받음
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = Settings.StartMoney
	money.Parent = leaderstats
end)

-- 적이 기지에 도착함
local function damageBase(amount)
	local health = math.max(0, workspace:GetAttribute("BaseHealth") - amount)
	workspace:SetAttribute("BaseHealth", health)
	print("Base: " .. health)
end

-- 적을 경로를 따라 이동시킴. MoveTo는 8초 후 이동을 포기하고 이를 알림
-- (MoveToFinished가 false를 반환함). 따라서 느린 적은 도착할 때까지 다시 이동 명령을 받음.
local function walkPath(enemy)
	local humanoid = enemy.Humanoid
	for i = 2, #waypoints do
		repeat
			humanoid:MoveTo(waypoints[i].Position)
		until humanoid.MoveToFinished:Wait() or not enemy.Parent
		if not enemy.Parent then
			return
		end
	end
	-- 기지에 도착함
	damageBase(Settings.Enemies[enemy.Name].Damage)
	enemy:Destroy()
end

-- 입구에 나타나 경로를 따라 이동하는 새로운 적
local function spawnEnemy(kind)
	local enemy = ServerStorage.Enemies[kind]:Clone()
	-- 첫 번째 웨이포인트 바로 위에 배치됨
	enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
	local info = Settings.Enemies[kind]
	enemy.Humanoid.MaxHealth = info.Health
	enemy.Humanoid.Health = info.Health
	enemy.Humanoid.WalkSpeed = info.Speed
	enemy.Parent = enemies
	task.spawn(walkPath, enemy)
	return enemy
end

-- 한 웨이브: 적들을 하나씩 내보낸 뒤 모두 사라질 때까지 기다림
local function runWave(list)
	for _, kind in list do
		spawnEnemy(kind)
		task.wait(Settings.SpawnGap)
	end
	while #enemies:GetChildren() > 0 do
		task.wait(0.2)
	end
end

-- 각각 휴식 시간을 거친 후 시작되는 웨이브들
for i, list in Settings.Waves do
	task.wait(Settings.WaveBreak)
	runWave(list)
end

Roblox 게임: 타워 디펜스의 모든 레슨

직접 연습해 보세요: Roblox 플레이그라운드