PDA

View Full Version : (Share)[Lua]How to Script a Custom command



Noblebeastx
17-03-10, 10:17 PM
Lets say i want a SpawnNPC Command, where it should spawn my teleporter..

First start with a local function for the chat message, Like this:




local Teleporter_Spawn = "#TeleportSpawn"
The Teleporter_Spawn is a local which is reffering to something further down in your script.
Everything inside " " Are the command, which will say ingame they must now type #TeleportSpawn to spawn the teleporter!

Now you gona want the function, Thenwe start off like this:




function OnChat_TeleportSpawn(event, player, message, type, language)
the OnChat_TeleportSpawn is the function which will make it all work.

Now we need it to find the message in the chat and make the teleporter be spawned.

Then will tell the Lua what will happen if that message comes.




if (message == Teleporter_Spawn) then
of course we want our teleporter infront of us, so we need to get some coordinations.

then we will use these locals:




local x = player:GetX()local y = player:GetY()local z = player:GetZ()local o = player:GetO()
Now that will give us the correct cords of your possision. We want the teleporter to be spawned 2 yards infront of you.

then it is going to look like this:




player:SpawnCreature(76668, x, y+2, z, o, 35, 30000) player:SendAreaTriggerMessage("The Npc will be spawned for 30 sec.!")
76668 is my teleporter ID, then x is GetX(), y is GetY(), z is GetZ() and 0 is GetO()


35 is the faction, and 30000 is the spawn time,
so it would be like this:



:SpawnCreature(NPCID, x, y, z, o, Faction, SpawnTime(In milliseconds)
The SendAreaTriggerMessage send a message with yellow colour in the screen of the player



Of course we do not want this message to be shown in the chat when people type it, so we will add





return 0
right under SendAreaTriggerMessage.


Now the script is basicly done so then we must add "end" to it so it will work
end will be added 2 times in this script, since end is going to be there for every time u have written function or if

Lets say you have 2 function lines, and 3 if lines. then we are going to do it like this:




endendendendend
5 Times, since its 5 total
,

so here we are going to need 2 end Right after the return 0





player:SendAreaTriggerMessage("The Npc will be spawned for 30 sec.!") return 0 endend
at the end we need to register the function which will be done on commands by doing:





RegisterServerHook(16, "OnChat_TeleportSpawn")
To the end my Command Script looks like this:





local Teleporter_Spawn = "#TeleportSpawn"function OnChat_TeleportSpawn(event, player, message, type, language) if (message == Teleporter_Spawn) thenlocal x = player:GetX()local y = player:GetY()local z = player:GetZ()local o = player:GetO() player:SpawnCreature(76668, x+2, y, z, o, 35, 30000) player:SendAreaTriggerMessage("The Npc will be spawned for 30 sec.!") return 0 endendRegisterServerHook(16, "OnChat_TeleportSpawn")


Share plz dont Flame or QQ if u cant say some thing nice or helpful plz dont say it at all enless ure a mod or admin thank you


Credits go to Wx2 From Ac-Web thxs Man!