Lewati ke konten

7. Buat sistem upgrade

Di akhir materi ini, kamu akan bisa:

  • Menghubungkan client dan server menggunakan RemoteFunction di ReplicatedStorage.
  • Membuat JumpService di ServerScriptService untuk memproses upgrade lompatan.
  • Memvalidasi saldo koin di server sebelum menyetujui transaksi.
  • Memotong koin dengan PlayerData.updateValue dan memperbarui JumpPower karakter.
  • Menjaga nilai JumpPower tetap aktif saat karakter respawn dengan CharacterAdded.
  • Selesaikan materi Buat checkpoint.
  • Pastikan modul Leaderboard dan PlayerData sudah ada di ServerStorage.
  • Kumpulkan beberapa koin saat playtest untuk mencoba pembelian upgrade.

1. Siapkan RemoteFunction di ReplicatedStorage

Section titled “1. Siapkan RemoteFunction di ReplicatedStorage”

ReplicatedStorage adalah tempat menyimpan objek yang dapat diakses bersama oleh client (pemain) dan server.

  1. Di Explorer, arahkan kursor ke ReplicatedStorage, lalu buat Folder bernama Instances.
  2. Di dalam folder Instances, tambahkan sebuah RemoteFunction.
  3. Ubah namanya menjadi IncreaseJumpPowerFunction.

2. Buat JumpService di ServerScriptService

Section titled “2. Buat JumpService di ServerScriptService”
  1. Di ServerScriptService, buat sebuah Script baru.
  2. Beri nama JumpService.
  3. Isi kodenya dengan:
flowchart LR
classDef trigger fill:#0369a1,stroke:#38bdf8,stroke-width:2px,color:#ffffff;
classDef condition fill:#b45309,stroke:#f59e0b,stroke-width:2px,color:#ffffff;
classDef action fill:#047857,stroke:#10b981,stroke-width:2px,color:#ffffff;
classDef finish fill:#334155,stroke:#94a3b8,stroke-width:2px,color:#ffffff;
Invoke["<b>Panggilan Remote</b><br/>OnServerInvoke"]:::trigger
Check{"<b>Validasi Saldo</b><br/>Koin >= 5?"}:::condition
Upgrade["<b>Terapkan Upgrade</b><br/>Potong 5 koin & Jump +30"]:::action
UpdateBoard["<b>Update Skor</b><br/>Leaderboard.setStat"]:::action
Success(["<b>Sukses</b><br/>return true"]):::finish
Reject(["<b>Ditolak</b><br/>return false"]):::finish
Invoke --> Check
Check -- "Ya" --> Upgrade --> UpdateBoard --> Success
Check -- "Tidak" --> Reject

3. Trigger Pembelian dari Client / Interaksi

Section titled “3. Trigger Pembelian dari Client / Interaksi”

Untuk memanggil fungsi upgrade di server dari tombol UI (ScreenGui) atau skrip interaksi client di StarterPlayerScripts, gunakan pemanggilan InvokeServer():

flowchart LR
classDef trigger fill:#0369a1,stroke:#38bdf8,stroke-width:2px,color:#ffffff;
classDef condition fill:#b45309,stroke:#f59e0b,stroke-width:2px,color:#ffffff;
classDef action fill:#047857,stroke:#10b981,stroke-width:2px,color:#ffffff;
classDef finish fill:#334155,stroke:#94a3b8,stroke-width:2px,color:#ffffff;
Call["<b>InvokeServer</b><br/>Kirim permintaan beli"]:::trigger
Check{"<b>Respon Server</b><br/>Hasil boolean?"}:::condition
Success(["<b>Sukses</b><br/>'Upgrade berhasil!'"]):::action
Fail(["<b>Gagal</b><br/>'Koin kurang!'"]):::finish
Call --> Check
Check -- "true" --> Success
Check -- "false" --> Fail

Materi ini selesai jika:

  • IncreaseJumpPowerFunction berada di dalam ReplicatedStorage.Instances;
  • JumpService aktif di dalam ServerScriptService;
  • pembelian hanya berhasil jika koin pemain minimal 5;
  • saat upgrade dibeli, nilai Coins berkurang 5 dan Jump bertambah 30;
  • kekuatan lompatan tetap bertahan saat karakter respawn;
  • tidak ada pesan error merah di jendela Output.

Cobalah buat batasan maksimal level lompatan (misal maksimal JumpPower = 140) di dalam fungsi onPurchaseJumpIncrease agar karakter tidak melompat terlalu tinggi melompati seluruh peta.

Kamu telah membuat sistem upgrade client-server yang aman menggunakan RemoteFunction, JumpService, dan PlayerData. Seluruh validasi ekonomi dan peningkatan stat dikontrol penuh oleh server.