Skip to content

LuaAPI Functions

itvnnnd87 edited this page Oct 24, 2026 · 3 revisions

TSWMU LuaAPI function reference

The GameServer functions are grouped in tables. This page covers the server, objects, monsters, maps, messages and utilities.

Note

  • If the object does not exist, read functions return 0, "" or nil; write functions do nothing and return false.
  • Write functions only work on a character that is in game. Inside some game events they are blocked (see Game events).
  • Arguments of the wrong type are ignored: Object.SetPower(i, 0, nil) does not set the HP to 0.
  • An error inside a function never stops the GameServer; it stops only the script that called it and is written to the log.

Constants used below (STAT_*, POWER_*, ACTION_*, OBJECT_*, CLASS_*, MAP_*) are defined in System\Constants.lua.

Server

Function Description
Server.GetCode() GameServer code (ServerCode).
Server.GetName() GameServer name.
Server.GetOnlineCount() Characters in game on this GameServer.
Server.GetMaxUser() Maximum number of players.
Server.GetVersion() Client version accepted by the server ("1.04.xx").
Server.GetMode() 0 PvP, 1 non-PvP, 2 Golden PvP, 3 Golden non-PvP.
Server.IsNonPvP() true on a non-PvP server.
Server.GetClassPointsPerLevel(class) Level-up points of a character class.
Server.Is28Option() Always true (the +28 option is always on in this version).
Server.GetLanguage() Name of the language file in use (Lang\...).

Object

Works for every object: characters, monsters and NPCs.

Function Description
Object.GetUserRange() 2 values: lowest and highest character index.
Object.GetMonsterRange() 2 values: lowest and highest monster index.
Object.GetSummonRange() 2 values: lowest and highest summon index.
Object.GetMaxIndex() Highest object index.
Object.GetMaxItem() Maximum number of items on one map.
Object.GetType(index) 0 empty, 1 character, 2 monster, 3 NPC, 5 item on the ground.
Object.GetConnectStatus(index) 0 empty, 1 connected, 2 logged in, 3 in game.
Object.GetName(index) Name.
Object.GetClass(index) Character class / monster ID.
Object.GetMap(index), GetX, GetY Map and position.
Object.GetDirection(index) Direction 0..7.
Object.GetTarget(index) Index of the target being attacked, -1 if none.
Object.IsLive(index) true if alive.

HP, MP, SD, AG

power: POWER_LIFE (0), POWER_MANA (1), POWER_SHIELD (2), POWER_STAMINA (3, AG / BP).

Function Description
Object.GetPower(index, power) Current value.
Object.GetMaxPower(index, power) Maximum (base + added).
Object.GetAddPower(index, power) Added part of the maximum.
Object.SetPower(index, power, value) Sets the current value (0..maximum) and updates the client.
Object.SetMaxPower(index, power, value) Temporary: the next stat calculation (Player.ReCalc, changing items ...) recalculates it.

Combat stats

Object.GetCombatStat(index, name) / Object.SetCombatStat(index, name, value). A value set here is temporary: the next stat calculation recalculates it. For permanent bonuses use the option table.

Names
AttackDamageMin AttackDamageMax AttackDamageMinRight AttackDamageMaxRight AttackDamageMinLeft AttackDamageMaxLeft
MagicDamageMin MagicDamageMax CurseDamageMin CurseDamageMax
AttackRating AttackRatingPvP AttackSpeed MagicSpeed MoveSpeed
Defense DefensePvP DefenseRating DefenseRatingPvP MagicDefense
CriticalDamage ExcellentDamage DoubleDamage DamageDecrease ReflectDamage SkillDamage DamageAdd
ElementalDefense ElementalDamageMin ElementalDamageMax ElementalAttackRating ElementalDefenseRating
DebuffResistance DebuffDefense

Actions

Function Description
Object.Warp(index, x, y) Moves the object inside its current map.
Object.Kill(index) Kills a character or monster.
Object.ViewportCreate(index) Shows the object again to the players around it.
Object.ViewportDestroy(index) Hides the object from the players around it.

Loops

The callback receives the object index. Return false to stop the loop. An error inside the callback stops the loop and is reported to the calling script.

Function Objects
Object.ForEachPlayer(callback) Characters in game.
Object.ForEachPlayerOnMap(map, callback) Characters in game on one map.
Object.ForEach(callback) Every character, monster and NPC.
Object.ForEachMonster(callback) Every monster.
Object.ForEachMonsterOnMap(map, callback) Monsters on one map.
Object.ForEachMonsterByClass(class, callback) Monsters of one class.
Object.ForEachPartyMember(partyNumber, callback) Party members online on this GameServer.
Object.ForEachGuildMember(guild, callback) Guild members online on this GameServer. guild: guild number or name.
Object.ForEachNearby(index, range, callback) Characters and monsters on the same map within range cells.
Object.CountPlayersOnMap(map) Number of characters on a map.
Object.CountMonstersOnMap(map, class) Number of monsters on a map. Without class: every class.
Object.ForEachPlayerOnMap(MAP_LORENCIA, function(aIndex)
    Message.Send(aIndex, 0, "The Lorencia event has started!")
end)

Monster

Function Description
Monster.Create(class, map, x, y, dir, moveRange, despawnSeconds, instance) Spawns a monster, returns its index (-1 on error). dir -1 random, moveRange -1 from the monster data, despawnSeconds 0 stays until killed. Only class, map, x, y are required.
Monster.CreateInArea(class, map, x1, y1, x2, y2, count, despawnSeconds) Spawns count monsters at random points of the area. Returns how many were spawned.
Monster.CreateEx(class, map, x, y, dir, moveRange, life, damageMin, damageMax, defense, attackRate, defenseRate) Spawns a monster with custom stats.
Monster.Delete(index) Removes a monster spawned by a script or a GM command. Monsters that belong to the map cannot be removed (false).
Monster.GetTopDamageDealer(index) Character that did the most damage, -1 if none.
Monster.GetAttr(class) Monster data table (Monster.xml) or nil. Fields below.
Monster.WatchRespawn(class, watch) Turns the onMonsterRespawn event on (true) or off (false) for one class.

Monster.GetAttr fields: Class Name Level Life Mana DamageMin DamageMax Defense AttackRate DefenseRate MoveRange AttackRange ViewRange AttackSpeed RespawnTimeMin RespawnTimeMax ItemRate ZenRate ElementalAttribute ScriptName.

Skill

Function Description
Skill.Watch(skill, watch) Turns the onUseSkill event on (true) or off (false) for one skill.

Party

Function Description
Party.GetCount(partyNumber) Number of members.
Party.GetMember(partyNumber, n) Index of member n (1..5, 1 = leader). -1 if empty or not online on this GameServer.

Use Player.GetPartyNumber(aIndex) to get the party of a character.

Map

Function Description
Map.GetName(map) Map name.
Map.Exists(map) true if the map is hosted on this GameServer.
Map.GetAttr(map, x, y) Cell attribute bits: 1 safe zone, 2 standing, 4 / 8 / 16 blocked.
Map.CheckAttr(map, x, y, attr) true if the cell has the attribute attr.
Map.Fireworks(map, x, y) Fireworks for the players near the point.

Message

Message type: 0 gold (screen centre), 1 blue, 2 guild, 3 red.

Function Description
Message.Send(aIndex, type, text) To one player.
Message.SendAll(type, text) To everyone on this GameServer.
Message.SendGlobal(type, text) To every GameServer (through Server_Link).
Message.Box(aIndex, type, title, text) Message box in the screen centre.
Message.BoxAll(type, title, text) Message box for everyone on this GameServer.
Message.NpcSay(npcIndex, aIndex, text) An NPC talks to one player; aIndex = -1: to everyone near the NPC.
Message.Post(name, text) A /post style line for everyone on this GameServer (text up to 200 bytes).
Message.GetText(id, default) Text id of the server language file, default if missing.

Texts longer than 1024 bytes are cut. See Languages for non-English text.

Config (INI)

Function Description
Config.ReadNumber(section, key, path, default) Number from an INI file.
Config.ReadString(section, key, path, default) Text from an INI file.
Config.SaveString(section, key, value, path) Writes a value. Only files inside the Data folder can be written.

Paths work like File.* (see Config files).

Log, Util, Event

Function Description
Log.Add(text) Writes to the GameServer log.
Log.AddC(color, text) 1 = red (error), 3 = yellow (warning), anything else = normal.
Util.Random(min, max) Random integer in [min, max].
Util.LargeRandom() Random integer in [0, 4294967295].
Util.GetTick() Milliseconds since the machine started.
Util.ArgNumber(text, pos) Argument at pos (starting at 0) as a number.
Util.ArgString(text, pos) Argument at pos as a string.
Event.Refresh() Tells the GameServer which on... events now exist. Call it after you create an event function at run time (BridgeFunctionAttach does it for you).

Next: Player functions

Clone this wiki locally