PtokaX forum

Development Section => LUA & PtokaX-Scripting-Interface => Topic started by: Yahoo on 01 May, 2007, 14:07:38

Title: Scripting Help
Post by: Yahoo on 01 May, 2007, 14:07:38
i had join this forum long back for requesting script and now i want to try scripting which can be possible only with the help of all members and scripters of this forum.So i will like to have support of all the members of this forum and this can also help new scripter who have started there scripting recently or want to start with it and it can also reduce the pressure on most of the scripter
I WILL LIKE TO KNOW
1)how can i learn LUA language, which stuff to read or refer to learn this language.
2)i would also like to know the difference between lua 4, 5, 5.1


P.S  i dont know anything about programing or scripting

thanks in advance
Title: Re: Scripting Help
Post by: bastya_elvtars on 01 May, 2007, 14:16:31
So, you'd like to start programming in Lua. Your holy bible should be:
- Lua book (http://www.lua.org/pil/)
- 5.1 reference manual (http://www.lua.org/manual/5.1/)
- PtokaX Wiki (http://wiki.ptokax.ath.cx/doku.php)
Title: Re: Scripting Help
Post by: 6Marilyn6Manson6 on 01 May, 2007, 14:18:11
www.lua.org

You find all answers to your questions :D
Title: Re: Scripting Help
Post by: achiever on 01 May, 2007, 16:10:55
if i may suggest its also good to look into others script and modify things and c how they work.
still i m no one to even suggest u this, but can be helpful ;)
Title: Re: Scripting Help
Post by: Yahoo on 01 May, 2007, 16:54:51
thanks all members for u reply on my post
Quoteif i may suggest its also good to look into others script and modify things and c how they work.
still i m no one to even suggest u this, but can be helpful Wink
i have tried modifying and i can do it upto a extent by then also i am not able to write a script . i just dont qwanna modify scripts i want to create scripts
Title: Re: Scripting Help
Post by: achiever on 01 May, 2007, 16:58:12
Quote from: Yahoo on 01 May, 2007, 16:54:51
i just dont qwanna modify scripts i want to create scripts
u r not alone in this.
Title: Re: Scripting Help
Post by: bastya_elvtars on 02 May, 2007, 01:20:33
And also we encourage lua-related discussion here, it's kinda boring to code 'can I have this in PM' issues all the time.
Title: Re: Scripting Help
Post by: Yahoo on 02 May, 2007, 14:26:20
thanks you all the member for your support. the site and the reference manual suggested by the scripter and members are very helpful but the prob is tht they are to big and vast (boaring also) to read with my speed i will require atleast 2 months to read tht whole thing i cant manage tht much time. so can u suggest me any other way through which i can learn the language in small,simple and easy way


thanks in advance
Title: Re: Scripting Help
Post by: bastya_elvtars on 02 May, 2007, 14:29:04
I started with creating a simple bot. You should create one too:
Title: Re: Scripting Help
Post by: speedX on 03 May, 2007, 07:54:01
Quote from: bastya_elvtars on 02 May, 2007, 14:29:04
I started with creating a simple bot. You should create one too:

  • register at startup
  • when a new user connects, greet her
  • when a user disconnects, say goodbye

Done :D
Title: Re: Scripting Help
Post by: achiever on 03 May, 2007, 18:50:10
Quote from: bastya_elvtars on 02 May, 2007, 14:29:04
I started with creating a simple bot. You should create one too:

  • register at startup
  • when a new user connects, greet her
  • when a user disconnects, say goodbye

here is my try
sBot ="Fusion"
mesg ="this hub welcomes all regs. Hope u enjoy ur stay"
mesg1 ="Good bye!! c u soon"
function NewUserConnected(user)
user:SendData(sBot, mesg)
end
OpConnected = NewUserConnected

function UserDisconnected(user)
SendToAll(sBot, mesg1)
end
OpDisconnected = UserDisconnected


how is it, i will like to here all suggestions but plzz be abit explainatory ;)
how do u register a bot?

i even tried to do this profile specific
sBot ="Fusion"
mesg ="this hub welcomes all regs. Hope u enjoy ur stay"
mesg1 ="Good bye!! c u soon"
mesg2 ="welcome home"
function NewUserConnected(user)
if user.iProfile ==0 or user.iProfile ==1 then
user:SendData(sBot, mesg2)
else
user:SendData(sBot, mesg)
end
end

function UserDisconnected(user)
SendToAll(sBot, mesg1)
end
OpDisconnected = UserDisconnected


it doesnt work :( what is the change needed.

thks,
Title: Re: Scripting Help
Post by: bastya_elvtars on 03 May, 2007, 19:28:01
If you check whether a user is registered, bRegistered would be a better choice. Registering a bot: hmm, documented.
http://wiki.ptokax.ath.cx/doku.php?id=scriptinghelp:ptokaxapi#the_frmhub_object
Title: Re: Scripting Help
Post by: Naithif on 03 May, 2007, 19:28:46
Quote from: achiever on 03 May, 2007, 18:50:10
function NewUserConnected(user)
if user.iProfile ==0 or user.iProfile ==1 then



Hint (SPOILER WARNING!!!)  :)
This'll never be true in itself, you need to do is similarly to disconnection

Quote from: achiever on 03 May, 2007, 18:50:10
OpDisconnected = UserDisconnected

Title: Re: Scripting Help
Post by: CrazyGuy on 03 May, 2007, 22:43:40
Quote from: achiever on 03 May, 2007, 18:50:10
here is my try
sBot ="Fusion"
mesg ="this hub welcomes all regs. Hope u enjoy ur stay"
mesg1 ="Good bye!! c u soon"
function NewUserConnected(user)
user:SendData(sBot, mesg)
end
OpConnected = NewUserConnected

function UserDisconnected(user)
SendToAll(sBot, mesg1)
end
OpDisconnected = UserDisconnected


This code is actually quite close. It doesn't work because the bot isn't registered, and most clients ignore messages from offline users.

To register your bot:


Main = function()
      frmHub:RegBot(sBot)
end


that should do it :)
Title: Re: Scripting Help
Post by: bastya_elvtars on 03 May, 2007, 23:05:01
I think they don't benefit from directly telling them the solution, we should rather give them hnts.
Title: Re: Scripting Help
Post by: 6Marilyn6Manson6 on 03 May, 2007, 23:23:21
Quote from: CrazyGuy on 03 May, 2007, 22:43:40
This code is actually quite close. It doesn't work because the bot isn't registered, and most clients ignore messages from offline users.

To register your bot:


Main = function()
      frmHub:RegBot(sBot)
end


that should do it :)

For speedX example:

function Main()
      frmHub:RegBot(sBot)
end


This version is most good :p
Title: Re: Scripting Help
Post by: Naithif on 03 May, 2007, 23:37:14
If only the second version is the one that's not working (as I've suspected), then see my post. The second version misses a line...
Title: Re: Scripting Help
Post by: 6Marilyn6Manson6 on 03 May, 2007, 23:38:51
Quote from: Mutor on 03 May, 2007, 23:31:22There is no functional difference here.

Sure? but speedX has been only used  mode  funtion name()   only for this my version would be more homogenous ^^
Title: Re: Scripting Help
Post by: speedX on 04 May, 2007, 10:43:20

Bot = frmHub:GetHubBotName()


function Main()
frmHub:RegBot(Bot)
end

function NewUserConnected(user)
  if user.bOperator then
    user:SendData(Bot, "Welcome home "..user.sName)
  else
    user:SendData(Bot, "Welcome "..user.sName.." to "..frmHub:GetHubName())
  end
end
OpConnected = NewUserConnected

function UserDisconnected(user)
  SendToAll(Bot, "Goodbye "..user.sName)
end
OpDisconnected = UserDisconnected
Title: Re: Scripting Help
Post by: achiever on 04 May, 2007, 11:41:54
Quote from: Naithif on 03 May, 2007, 19:28:46
Hint (SPOILER WARNING!!!)  :)
This'll never be true in itself, you need to do is similarly to disconnection

naithif i did not understand this plzz explain it.

Quote from: bastya_elvtars on 03 May, 2007, 19:28:01
Registering a bot: hmm, documented.
http://wiki.ptokax.ath.cx/doku.php?id=scriptinghelp:ptokaxapi#the_frmhub_object
thks bastya this was really a good 1 :)
Title: Re: Scripting Help
Post by: speedX on 04 May, 2007, 12:12:34
Quote from: bastya_elvtars on 02 May, 2007, 14:29:04
I started with creating a simple bot. You should create one too:

  • register at startup
  • when a new user connects, greet her
  • when a user disconnects, say goodbye

Next ??
Title: Re: Scripting Help
Post by: achiever on 04 May, 2007, 12:33:10
well i m trying to add some more messages and it should send random message
Title: Re: Scripting Help
Post by: Naithif on 05 May, 2007, 01:16:03
Quote from: achiever on 03 May, 2007, 18:50:10

i even tried to do this profile specific
sBot ="Fusion"
mesg ="this hub welcomes all regs. Hope u enjoy ur stay"
mesg1 ="Good bye!! c u soon"
mesg2 ="welcome home"
function NewUserConnected(user)
if user.iProfile ==0 or user.iProfile ==1 then
user:SendData(sBot, mesg2)
else
user:SendData(sBot, mesg)
end
end

function UserDisconnected(user)
SendToAll(sBot, mesg1)
end
OpDisconnected = UserDisconnected


Here's your post.
You got a NewUserConnected function that's targets are users. (user, reg, vip)
You got a profile check that's targets are masters, and ops

Hint 1
NewUserConnected takes all users, regs, and vips, and the if statement filters everyone of them except ops and masters.  ::)


Hint 2
You are missing a line that was present in version 1...

Title: Re: Scripting Help
Post by: achiever on 05 May, 2007, 10:21:35
well yes got it
i had missed OpConnected = NewUserConnected

thks
Title: Re: Scripting Help
Post by: CrazyGuy on 05 May, 2007, 13:12:59
Quote from: Naithif on 05 May, 2007, 01:16:03

Hint 1
NewUserConnected takes all users, regs, and vips, and the if statement filters everyone of them except ops and masters.  ::)


This is not exactly true.
NewUserConnected handles all users with a profile with bIsOP = 0 , and unregistered users.
Subtle, yet important difference if you use custom profile permissions  ::)
Title: Re: Scripting Help
Post by: Naithif on 05 May, 2007, 14:47:50
Quote from: CrazyGuy on 05 May, 2007, 13:12:59
This is not exactly true.
NewUserConnected handles all users with a profile with bIsOP = 0 , and unregistered users.
Subtle, yet important difference if you use custom profile permissions  ::)

By default, me refers to default profile setting (;D)
Anyway, in 99% this is the case, also if robocop, or leviathan profiles are in use
Title: Re: Scripting Help
Post by: bastya_elvtars on 05 May, 2007, 16:20:47
Should I install the 'spoiler tag' plugin for howto's? ;D
Title: Re: Scripting Help
Post by: Yahoo on 17 May, 2007, 17:25:03
if i want to display the date in the following format
Thursday, May 17, 2007 19:47
what should be my code
Title: Re: Scripting Help
Post by: Thor on 17 May, 2007, 18:42:40
Code (lua) Select
os.date("%A, %b %d, %Y %H:%M")
Title: Re: Scripting Help
Post by: Yahoo on 17 May, 2007, 19:49:20
thanks for the code i will test it later
can any 1 tell me wat is wrong with the following code
sBot = frmHub:GetHubBotName()

ChatArrival = function(user,data)
local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
local tCmds = {
["time"] = function(user,data)
return user:SendData(sBot,os.date("Time: %I:%M:%S %p   Date: %A, %b %d, %Y "))
end
end,
["date"] = function(user,data)
return user:SendData(sBot,os.date("Date: %A, %b %d, %Y   Time: %I:%M:%S %p "))
end
end,
}

if tCmds[cmd] then
return tCmds[cmd](User, data)
end
end

end
ToArrival = ChatArrival

i am getting the following error
zztime bot.lua:19: '}' expected (to close '{' at line 15) near 'end'
Title: Re: Scripting Help
Post by: Naithif on 17 May, 2007, 21:17:01
Quote from: Yahoo on 17 May, 2007, 19:49:20
thanks for the code i will test it later
can any 1 tell me wat is wrong with the following code
sBot = frmHub:GetHubBotName()

ChatArrival = function(user,data)
local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
local tCmds = {
["time"] = function(user,data)
return user:SendData(sBot,os.date("Time: %I:%M:%S %p   Date: %A, %b %d, %Y "))
end
end,
["date"] = function(user,data)
return user:SendData(sBot,os.date("Date: %A, %b %d, %Y   Time: %I:%M:%S %p "))
end
end,
}

if tCmds[cmd] then
return tCmds[cmd](User, data)
end
end

end
ToArrival = ChatArrival

i am getting the following error
zztime bot.lua:19: '}' expected (to close '{' at line 15) near 'end'

Hint:
You're trying to close too many times ;)
Title: Re: Scripting Help
Post by: Yahoo on 18 May, 2007, 07:00:35
still not getting. can u tell me my mistake
Title: Re: Scripting Help
Post by: Naithif on 18 May, 2007, 10:44:30
@ B?stya -> Hints are just not working

(And next time if someone got a problem, then paste the script you got the error with, as line 15 is 'end,' no '{'-s there
Quotezztime bot.lua:19: '}' expected (to close '{' at line 15) near 'end'

Your problem is:


Spoiler warning!



'end' used too many times

Title: Re: Scripting Help
Post by: Yahoo on 18 May, 2007, 11:06:48
thanks sir i got the problem
but now i am getting a new error
zztime bot.lua:17: attempt to index global 'user' (a nil value)
zztime bot.lua:20: attempt to index global 'user' (a nil value)
can any1 giv me hint
the edited script looks like this
sBot = frmHub:GetHubBotName()

ChatArrival = function(user,data)
local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
local tCmds = {
["time"] = function(user,data)
return user:SendData(sBot,os.date("Time: %I:%M:%S %p   Date: %A, %b %d, %Y "))
end,
["date"] = function(user,data)
return user:SendData(sBot,os.date("Date: %A, %b %d, %Y   Time: %I:%M:%S %p "))
end,
}

if tCmds[cmd] then
return tCmds[cmd](User, data)
         end
        end
end
ToArrival = ChatArrival
Title: Re: Scripting Help
Post by: Yahoo on 18 May, 2007, 13:21:23
sorry sir i didnt get u can u tell a little more in brief and the full script
Title: Re: Scripting Help
Post by: Psycho_Chihuahua on 18 May, 2007, 13:27:19
as Mutor has said already LUA is Case Sensitive

so change
return tCmds[cmd](User, data)
to
return tCmds[cmd](user, data)

because User is NOT = user

sBot = frmHub:GetHubBotName()

ChatArrival = function(user,data)
local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
if cmd then
local tCmds = {
["time"] = function(user,data)
return user:SendData(sBot,os.date("Time: %I:%M:%S %p   Date: %A, %b %d, %Y "))
end,
["date"] = function(user,data)
return user:SendData(sBot,os.date("Date: %A, %b %d, %Y   Time: %I:%M:%S %p "))
end,
}

if tCmds[cmd] then
return tCmds[cmd](user, data)
         end
        end
end
ToArrival = ChatArrival


results in
[13:15:16] <Psycho_Chihuahua> time
[13:15:19] <Time> Time: 01:15:19 PM   Date: Friday, May 18, 2007
[13:15:19] <Psycho_Chihuahua> !time
[13:15:23] <Time> Date: Friday, May 18, 2007   Time: 01:15:23 PM
[13:15:23] <Psycho_Chihuahua> !date
Title: Re: Scripting Help
Post by: Yahoo on 18 May, 2007, 15:04:36
thanks for all the help
Title: Re: Scripting Help
Post by: speedX on 19 May, 2007, 06:44:05
Could someone plzz explain me this.

local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")

Title: Re: Scripting Help
Post by: achiever on 19 May, 2007, 09:23:19

local sBot,h,m = "Time",math.modf((os.time()-os.time(os.date"!*t"))/ 3600)
local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")

the second line is the capture pattern of strings

math.modf function is used, i think for geting the fraction and integer differently (its acually used to spilt the 2 parts if m not wrong)

os.date() this fuction returns the date and time

this all i can guess but,

local sBot,h,m = "Time",math.modf(([b]os.time()-os.time(os.date"!*t"[/b]))/ 3600)

what this ((os.time()-os.time(os.date"!*t"))/ 3600) is even i would like to understand

thks,
Title: Re: Scripting Help
Post by: achiever on 19 May, 2007, 09:56:30
now thats some explaination!!!

thks mutor