기지에 도달하기
Coddy의 Roblox 게임: 타워 디펜스 코스 레슨. 18개 중 3번째.
경로의 끝에 도달한 적은 기지에 피해를 줍니다. 기지의 체력은 서버에 있어야 하지만 모든 플레이어의 화면에 표시되므로, 워크스페이스의 속성으로 두세요. 서버에서 설정한 속성은 모든 클라이언트에서 볼 수 있습니다.
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
print(workspace:GetAttribute("BaseHealth")) --> 20damageBase(amount)는 체력을 0보다 낮아지지 않도록(math.max) 감소시킵니다. 마지막 웨이포인트에 도달한 뒤 적은 이 함수를 호출하고 Destroy를 사용해 게임에서 나갑니다.
챌린지
쉬움- 웨이포인트를 정렬한 후, workspace 속성
BaseHealth를Settings.BaseHealth로 설정하세요. damageBase(amount)를 작성하세요. 이 함수는BaseHealth를amount만큼 낮추며, 0보다 낮아지지 않도록 합니다.- 마지막 웨이포인트 이후
walkPath의 끝에서damageBase(1)을 호출하고 적을 제거하세요.
Play를 누른 다음 Check를 누르세요.
직접 해보기
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)
-- 모든 플레이어는 리더보드에 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)
-- 적을 경로를 따라 이동시킴. 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
end
-- 입구에 나타나 경로를 따라 이동하는 새 적
local function spawnEnemy(kind)
local enemy = ServerStorage.Enemies[kind]:Clone()
-- 첫 번째 경유 지점 바로 위에 배치됨
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
enemy.Parent = enemies
task.spawn(walkPath, enemy)
return enemy
end
-- 지금은 적 한 명만
spawnEnemy("Zombie")
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.