Lewati ke konten

Bab 9: Wasit Pertarungan & Damage (ServerScriptService > Services > CombatService)

Bagian 3 • Sistem Tempur

Modul ini adalah pusat perhitungan damage di game kita. Baik zombie menyerang pemain maupun pemain menembak zombie, perhitungannya diproses di sini agar tidak ada pemain yang bisa memanipulasi damage sesuka hati.

Langkah Pembuatan: Di dalam ServerScriptService > Services, klik + lalu pilih ModuleScript. Beri nama CombatService.

ServerScriptService > Services > CombatService (ModuleScript)
--!strict
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local Config = require(ReplicatedStorage:WaitForChild("Config") :: ModuleScript)
local Signal = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("Signal") :: ModuleScript)
local SoundManager = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("SoundManager") :: ModuleScript)
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local DamageIndicator = Remotes:WaitForChild("DamageIndicator") :: RemoteEvent
local BossHealthUpdated = Remotes:WaitForChild("BossHealthUpdated") :: RemoteEvent
local HitConfirm = Remotes:WaitForChild("HitConfirm") :: RemoteEvent
local PlayerDataService = require(script.Parent:WaitForChild("PlayerDataService") :: ModuleScript)
local CombatService = {}
CombatService.ZombieDied = Signal.new()
CombatService.PlayerTookDamage = Signal.new()
local function createHitBloodEffect(position: Vector3)
local part = Instance.new("Part")
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Position = position
part.Anchored = true
part.CanCollide = false
part.Transparency = 1
part.Parent = workspace
local attachment = Instance.new("Attachment")
attachment.Parent = part
local particles = Instance.new("ParticleEmitter")
particles.Color = ColorSequence.new(Color3.fromRGB(160, 10, 10), Color3.fromRGB(80, 0, 0))
particles.Size = NumberSequence.new({
NumberSequenceKeypoint.new(0, 0.6),
NumberSequenceKeypoint.new(1, 0.1)
})
particles.Transparency = NumberSequence.new(0, 0.8)
particles.Speed = NumberRange.new(5, 12)
particles.SpreadAngle = Vector2.new(180, 180)
particles.Lifetime = NumberRange.new(0.2, 0.45)
particles.Rate = 0
particles.Parent = attachment
particles:Emit(15)
Debris:AddItem(part, 1)
end
function CombatService.DamageZombie(attacker: Player?, zombieModel: Model, baseDamage: number, hitPart: BasePart?)
if not zombieModel or not zombieModel.Parent then return end
if zombieModel:GetAttribute("IsDead") then return end
local humanoid = zombieModel:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health <= 0 then return end
local zType = zombieModel:GetAttribute("ZombieType") or "Walker"
local zCfg = Config.Zombies[zType]
local isHeadshot = (hitPart ~= nil and hitPart.Name == "Head")
local finalDamage = baseDamage
if attacker then
local pData = PlayerDataService.Get(attacker)
local multiplier = pData and pData.DamageMultiplier or 1.0
finalDamage = baseDamage * multiplier
if isHeadshot then
finalDamage *= 1.5 -- Bonus Damage Headshot 1.5x
end
HitConfirm:FireClient(attacker, isHeadshot)
end
humanoid.Health = math.max(0, humanoid.Health - finalDamage)
local targetPart = hitPart or zombieModel:FindFirstChild("HumanoidRootPart") or zombieModel.PrimaryPart
if targetPart and targetPart:IsA("BasePart") then
createHitBloodEffect(targetPart.Position)
SoundManager.PlaySFX("BatHit", targetPart, 0.75)
end
-- Jika musuh adalah Boss, kabarkan sisa HP ke seluruh pemain
if zCfg and zCfg.IsBoss then
BossHealthUpdated:FireAllClients(zCfg.DisplayName, humanoid.Health, humanoid.MaxHealth)
end
-- Cek apakah zombie mati
if humanoid.Health <= 0 then
zombieModel:SetAttribute("IsDead", true)
if attacker then
local reward = zCfg and zCfg.RewardCash or 25
PlayerDataService.AddCash(attacker, reward)
end
CombatService.ZombieDied:Fire(zombieModel, attacker, zType)
for _, part in ipairs(zombieModel:GetDescendants()) do
if part:IsA("BasePart") then
part.CanCollide = false
end
end
task.delay(1.0, function()
if zombieModel and zombieModel.Parent then
zombieModel:Destroy()
end
end)
end
end
function CombatService.DamagePlayer(player: Player, amount: number, knockbackDir: Vector3?)
local data = PlayerDataService.Get(player)
if not data or not data.IsAlive then return end
local newHealth = math.max(0, data.Health - amount)
PlayerDataService.SetHealth(player, newHealth)
local char = player.Character
if char and knockbackDir then
local hrp = char:FindFirstChild("HumanoidRootPart") :: BasePart?
if hrp and not hrp.Anchored then
hrp:ApplyImpulse(knockbackDir)
end
end
DamageIndicator:FireClient(player, amount)
CombatService.PlayerTookDamage:Fire(player, amount)
end
return CombatService
  • Sistem Headshot 1.5x: Script mendeteksi apakah bagian tubuh yang tertembak bernama "Head". Jika mengenai kepala, damage dikalikan 1.5 dan crosshair client memunculkan warna merah!
  • Efek Partikel Darah: Saat zombie tertembak, partikel merah darah (ParticleEmitter) memancar dari titik peluru dan otomatis dihapus dalam 1 detik dengan Debris:AddItem.
  • Pemberian Hadiah Uang: Ketika HP zombie habis, pemain yang membunuh zombie langsung dihadiahi uang tunai sesuai jenis zombie yang dikalahkan.
  • Efek Terpental (Knockback): Saat diserang oleh Brute atau Boss Abomination, tubuh pemain didorong terpental ke belakang menggunakan gaya fisika ApplyImpulse.