Example: Server Script - Display a Message Upon Player Level-Up and Grant an Item at a Specific Level
Scripts/Servers/Hello.lua
functiononUnitLevelUp(target,level)if target.level ==50then--When a target reaches level 50, a notification will be broadcast to the entire server, and Item 1 will be distributed to the target. Server.SendCenterLabel(target.name ..'has ' .. level ..'become!') target.AddItem(1) elseif target.level ==55then--When the target reaches level 55, Skill 5 and 5,000 game money will be awarded. target.AddSkill(5) target.AddGameMoney(5000)endendServer.onUnitLevelUp.Add(onUnitLevelUp)
Example: Server Script - Broadcast a Message to All Players and Grant Rewards for PVP Kills
Scripts/Servers/Hello.lua
functiononUnitDead(target,attacker) --Target: Deceased, Attacker: Aggressorif (target.type==0and attacker.type==0) then--If it is a PlayerUnit Server.SendCenterLabel(target.name..'as \n'..attacker.name..'The attacker killed the target.') attacker.AddGameMoney(100) --100 gold is awarded to the attacker.ifrand(1, 100) <=50then attacker.AddItem(100) --The attacker has a 50% chance to receive Item 1.endendendServer.onUnitDead.Add(onUnitDead) -- Add the onUnitDead function to Server.onUnitDead.
Example: Using onJoinField - Display Player Name Upon Entering a Specific Map and Send a Server-Wide Message
functiononJoinField()functiononJoinField_1(Field,unit) unit.SendCenterLabel(Field.name) -- Temporarily displays field names to units.if#Field.playerUnits >10then-- # indicates the length of the table. Server.SendSay(Field.name.."More than 11 players gathered!")endendlocal map = Server.GetField(1)if map ~=nilthen map.onJoinField.Add(onJoinField_1)--When entering field 1, the onJoinField_1 function is called.endendServer.RunLater(onJoinField,1) -- Executes the onJoinField function after 1 second.-- The reason we run it after 1 second is to avoid referencing nil since the map may not have been created after the server runs.
onLeaveField - Handle actions when leaving a specific map
map - ScriptField
unit - ScriptUnit
File: Script Example for a Specific MapUsage Example: Decreasing the Value of a World Variable by 1 When Leaving the Map
Examples: Adding, Removing, and Modifying Item Options (Refer to ScriptUtility)
Please test the code by removing the applied comments (--). The script below was executed via a touch event - triggered through the script.
-- Get information about the first item in your inventory.local item = unit.player.GetItems()[1]local option1 = item.options[1]--Utility.SetItemOption(option1,option1.type,option1.statID,option1.value+1)--Server.SendItemUpdated(item)Utility.AddItemOption(unit.player.GetItems()[1],1,3,5)--unit.player.RemoveItemOption(item,1)unit.player.SendItemUpdated(item)
Example: Event Triggered Upon Joining a Party (playerJoinPartyCallback)
Server.playerJoinPartyCallback =function(scriptRoomPlayer,scriptParty) print(scriptRoomPlayer.unit.name)print(scriptParty.maxPlayer)-- A party will be created if return is set to true. You can specify return true or false depending on the condition.returntrueend
Example: Event Triggered Upon Leaving a Party (playerLeavePartyCallback)
Example: Event Triggered When Damage is Applied (damageCallback)
-- If the damage amount is 0, no damage is output.-- a,b ScriptUnitServer.damageCallback=function(a,b,damage,skillDataID,critical,visible) damage = a.atk - b.defif damage <=0then visible =falseendreturn damage, critical, visible;end
Example: Event Triggered Upon Completion of a Trade (onTradeDone)