A system
A system is a system script plus every script whose system line names it, like Wallet. Its types are written once, and its system script is the door other code goes through.
You'll learn a system script, a system type, export, and which side may use a system.
The system
In Shot, insert a ModuleScript named Wallet and mark it as ShotScript. Mark as ShotScript writes its system line for you, system script, which makes Wallet a system script. Then, in ShotScript Editor's Properties, set Wallet's RunContext property to Server.
system script use Format export Reason «Every player's coins, kept on the server.» «Why a player's coins changed.» system type Reason = "Pickup" | "Shop" module Wallet = { function Give(player : Player, amount : number, reason : Reason) local stats = player:FindFirstChild("leaderstats") local coins = if stats then stats:FindFirstChild("Coins") else nil if coins and coins:IsA("IntValue") then coins.Value += amount print(player.Name, reason, Format.Coins(coins.Value)) end end } return Wallet
system scriptmakes Wallet a system script, and a script's system line is what puts it in a system. Where a script sits never decides.Reasonis asystem type: it is written once and is bare in every script of the system.- A
« »on the line above a type documents it. The script's own doc is its first« », with a blank line after it. use Formatfinds the nearest Format: here, in Wallet's folder.return Wallethands out the module Wallet, and in a system script that is the system's API, the door other code goes through. Its RunContext property makes it server code: the system line takes no side word.- Wallet sits in ReplicatedStorage, where clients could reach it, so its server code compiles hidden: clients get only a stub.
export Reasonhands the type out. Outside the system it isWallet.Reason, or a bareReasonin code that uses Wallet.
Using it
The Server gives every new player 10 coins. It belongs to no system, so it reaches Wallet through Wallet's system script, by its path from game:
use game.ReplicatedStorage.Shot.Wallet local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player : Player) local stats = Instance.new("Folder") stats.Name = "leaderstats" local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Parent = stats stats.Parent = player Wallet.Give(player, 10, "Pickup") end)
Watch out Wallet is server code, so client code can't use it. A use of Wallet in the Client is an error at its line.
