Bab 12: Pengatur Gelombang Serangan (ServerScriptService > Services > WaveService)
Bagian 3 • Alur Gelombang
Modul ini bertugas menjadwalkan kapan musuh muncul satu per satu ke dalam arena. Dengan jeda spawn 1.0 detik, pemain tidak akan langsung dikeroyok oleh 10 zombie dalam 1 detik, melainkan musuh berdatangan secara bertahap seperti di film-film survival.
Langkah Pembuatan: Di dalam ServerScriptService > Services, klik + lalu pilih ModuleScript. Beri nama WaveService.
--!strictlocal CollectionService = game:GetService("CollectionService")local ReplicatedStorage = game:GetService("ReplicatedStorage")local Players = game:GetService("Players")
local Config = require(ReplicatedStorage:WaitForChild("Config") :: ModuleScript)local Signal = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("Signal") :: ModuleScript)local Remotes = ReplicatedStorage:WaitForChild("Remotes")local WaveInfoUpdated = Remotes:WaitForChild("WaveInfoUpdated") :: RemoteEvent
local EnemyService = require(script.Parent:WaitForChild("EnemyService") :: ModuleScript)local CombatService = require(script.Parent:WaitForChild("CombatService") :: ModuleScript)
local WaveService = {}WaveService.WaveCleared = Signal.new()WaveService.WaveStarted = Signal.new()
local currentWaveIndex = 0local remainingToSpawn: { string } = {}local aliveZombiesCount = 0local totalInWave = 0local isWaveRunning = falselocal spawnThread: thread? = nil
local function getEnemySpawnPoints(): { BasePart } local tagged = CollectionService:GetTagged("EnemySpawn") local parts: { BasePart } = {} for _, inst in ipairs(tagged) do if inst:IsA("BasePart") then table.insert(parts, inst) end end return partsend
local function syncWaveInfo() local remainingTotal = #remainingToSpawn + aliveZombiesCount WaveInfoUpdated:FireAllClients(currentWaveIndex, remainingTotal, totalInWave)end
function WaveService.Init() CombatService.ZombieDied:Connect(function(_zModel, _killer, _zType) if isWaveRunning then aliveZombiesCount = math.max(0, aliveZombiesCount - 1) syncWaveInfo()
if #remainingToSpawn == 0 and aliveZombiesCount == 0 then isWaveRunning = false WaveService.WaveCleared:Fire(currentWaveIndex) end end end)end
function WaveService.StartWave(waveNum: number) currentWaveIndex = waveNum local waveConfig = Config.Waves[waveNum] if not waveConfig then warn("[WaveService] No config for wave " .. waveNum) return end
table.clear(remainingToSpawn) for zType, count in pairs(waveConfig) do for _ = 1, count do table.insert(remainingToSpawn, zType) end end
-- Acak urutan kemunculan musuh for i = #remainingToSpawn, 2, -1 do local j = math.random(i) remainingToSpawn[i], remainingToSpawn[j] = remainingToSpawn[j], remainingToSpawn[i] end
totalInWave = #remainingToSpawn aliveZombiesCount = 0 isWaveRunning = true
WaveService.WaveStarted:Fire(waveNum) syncWaveInfo()
local spawnPoints = getEnemySpawnPoints() if #spawnPoints == 0 then warn("[WaveService] No EnemySpawn points found!") return end
if spawnThread then task.cancel(spawnThread) spawnThread = nil end
-- Loop kemunculan berkala dengan jeda 1.0 detik spawnThread = task.spawn(function() while isWaveRunning and #remainingToSpawn > 0 do local currentActive = EnemyService.GetActiveCount() if currentActive < Config.Game.MaxActiveZombies then local nextZombieType = table.remove(remainingToSpawn, 1) if nextZombieType then local sp = spawnPoints[math.random(1, #spawnPoints)] local spawned = EnemyService.SpawnEnemy(nextZombieType, sp.CFrame) if spawned then aliveZombiesCount += 1 syncWaveInfo() end end end task.wait(Config.Game.SpawnInterval) end end)end
function WaveService.StopWave() isWaveRunning = false if spawnThread then task.cancel(spawnThread) spawnThread = nil end table.clear(remainingToSpawn) aliveZombiesCount = 0 EnemyService.ClearAllEnemies() syncWaveInfo()end
function WaveService.ClearWaveInstantly() table.clear(remainingToSpawn) local killer = Players:GetPlayers()[1] for _, z in ipairs(EnemyService.GetActiveZombies()) do CombatService.DamageZombie(killer, z, 999999, z:FindFirstChild("Head") :: BasePart?) end aliveZombiesCount = 0 syncWaveInfo() if isWaveRunning then isWaveRunning = false WaveService.WaveCleared:Fire(currentWaveIndex) endend
function WaveService.GetCurrentWave(): number return currentWaveIndexend
function WaveService.IsRunning(): boolean return isWaveRunningend
function WaveService.GetRemainingZombies(): number return #remainingToSpawn + aliveZombiesCountend
return WaveServicePenjelasan Alur Wave:
Section titled “Penjelasan Alur Wave:”WaveService.StartWave(waveNum): Membaca daftar musuh dariConfig.Waves, mengacak urutannya, dan memulai thread pemunculan zombie secara berkala.SpawnInterval = 1.0s: Setiap 1 detik, 1 zombie dimunculkan di salah satu dari 4 titikEnemySpawnsecara acak.Deteksi Wave Selesai: Setiap kali zombie mati, sisa musuh dihitung mundur. Begitu antrean habis dan semua zombie di arena mati, sinyalWaveClearedotomatis dibunyikan ke GameManager!