Small patterns you will reuse in real uScript commands
These examples cover common command and event patterns you can adapt into real uScript scripts.
Allow Console To Use Commands
By default, commands are player-only. If you want the in-server console to run a command too, change the caller rules explicitly.
What Matters
- Set
allowedCallerto"player","console", or"both"depending on who should be allowed to execute the command. - If a command can be called from both places, you usually need separate output behavior for players and the console.
- A player command can reply with
player.message(...), while console usage typically logs throughlogger.log(...).
Things To Watch
- When the console runs the command,
playeris not set because the console is not a player. - Guard player-specific logic by checking whether
player != nullbefore using player methods or properties. - If the command only makes sense in-game, keep it player-only instead of trying to support both callers unnecessarily.
Example: command usable by players and console
command whoami(){
permission = "whoami";
allowedCaller = "both";
execute(){
if(player != null){
player.message("You're a player!");
}
else{
logger.log("You're the console!");
}
}
}
Get A Player From Name Or SteamID
To turn user input into a player object, use <code>toPlayer(...)</code> and then validate the result before touching it.
What Matters
- Use
toPlayer(<string>)when a command argument might be a player name or Steam64 ID. - Treat the result as optional and check whether the conversion returned a player before continuing.
- This pattern is useful for moderation commands, utility tools, and any script that targets another online player.
Things To Watch
- The target player must be online for this lookup to work.
- Validate the argument itself first so your command can fail gracefully when nothing was provided.
- If conversion fails, return a clear error message instead of assuming the input matched someone.
Example: converting a command argument to a player
command convert(myArgument){
permission = "convert";
execute(){
if(myArgument == null){
player.message("This command requires an argument!");
return;
}
targetPlayer = toPlayer(myArgument);
if(targetPlayer != null){
player.message("Successfully converted to a player.");
}
else{
player.message("Failed to convert to a player.");
}
}
}
Make Something Happen Every X Seconds
Repeated logic belongs in <code>onInterval</code>. It is the event used when you want the script to run a task every fixed number of seconds.
What Matters
- Use
event onInterval(<seconds>)to execute logic on a schedule. onIntervalis the event where you provide the interval argument directly in the event declaration.- Short intervals are good for lightweight checks; longer intervals are better for rewards, cleanup tasks, or recurring announcements.
Things To Watch
- Avoid doing heavy work too frequently unless you know the script cost is acceptable.
- If you use
foreach(player in server.players), be aware thatplayerbecomes your loop variable inside that block. - When a script already has a meaningful
playervariable in scope, rename the loop variable to something likeserverPlayerto avoid confusion or accidental overwrites.
Example: broadcast every 5 seconds and reward online players every 10 minutes
event onInterval(5){
broadcast("This message is broadcast every 5 seconds.");
}
event onInterval(600){
foreach(serverPlayer in server.players){
serverPlayer.experience += 100;
serverPlayer.message("You earned 100 experience for playing on the server!", "yellow");
}
}