Step 6 of 8, updated

use and import

use brings in a whole system. import brings in only its types, so a client system can name a server system's type.

You'll learn one system using another, a client system importing a type, and allow.

Use

Shop, a server system, spends coins through Wallet. In Shot, insert a ModuleScript named Shop, mark it as ShotScript, and set its RunContext property to Server in ShotScript Editor's Properties:

ReplicatedStorage/Shot/Shop.shot
system script

use Wallet

«Something a player can buy.»
system type Item = "Sword" | "Hat"

module Shop = {
	function Buy(player : Player, item : Item, price : number)
		Wallet.Give(player, -price, "Shop")
	end
}

return Shop
  • use Wallet finds the nearest Wallet, in Shop's folder. It gives Shop Wallet's functions, and the types Wallet exports come in bare, so "Shop" is checked as a Reason.
  • Shop and Wallet are both server code, so Shop may use Wallet.
  • Item appears in Buy's signature, so it is a system type. A plain type line is its script's own and can't appear in a module's member signatures.

Import

Hud, a client system, writes a line for the screen. In Shot, insert a ModuleScript named Hud, mark it as ShotScript, and set its RunContext property to Client in ShotScript Editor's Properties:

ReplicatedStorage/Shot/Hud.shot
system script

import Reason from script.Parent.Wallet

module Hud = {
	function Line(amount : number) : string
		local why : Reason = if amount < 0 then "Shop" else "Pickup"
		return why .. ": " .. tostring(amount)
	end
}

return Hud
  • import Reason from script.Parent.Wallet brings in the type and nothing else, so a client system may take it from a server system.
  • Only what Wallet's system script exports can be imported. Without export Reason in Wallet, Hud can't reach the type.

Watch out A use Wallet in Hud is an error at its line, the same as in the Client: client code can't use server code.

The Client uses Hud:

StarterPlayer/StarterPlayerScripts/Client.shot
use game.ReplicatedStorage.Shot.Format
use game.ReplicatedStorage.Shot.Hud

local Players = game:GetService("Players")

print(Players.LocalPlayer.Name, "starts with", Format.Coins(0))
print(Hud.Line(10))

Allow

Code outside a system reaches only its system script. When two systems need to share one script, that script's header can let the other system in. This place doesn't need one, but this is how it reads:

ReplicatedStorage/Shot/Wallet/Ledger.shot
system script.Parent
allow Shop

module Ledger = {
	public Spent : number = 0
}

return Ledger
ReplicatedStorage/Shot/Shop.shot
use script.Parent.Wallet.Ledger
  • Ledger is a member of Wallet. allow Shop lets every script of Shop reach it directly: its public values and types. Without that line, Shop's use of Ledger is an error.
  • allow goes in the script's header, at the start of its line, one system per line. It lets two systems share one script without a use loop between them.