Itutorial
Legendary OT User
- Joined
- Dec 23, 2014
- Messages
- 2,334
- Solutions
- 68
- Reaction score
- 1,012
Hello Ot fanatics. This thread is meant to help you understand a little more about lua scripting. If you don't know already. You must know how to script basic lua.
The first thing I want to explain:
When using a script to call a value the value must be defined.
What I mean is if the script calls a number. Like this.
if string > 0 then
string = text or letters.
0+ = a number value
If you do not define string as a number the script will give an error and will never work.
in TFS to define strings or values you use these commands.
tonumber(x) -- defines it as a number
tostring(x) -- defines x as a string
So it would be something like this.
value = tonumber(x)
Now if you wright:
value > 0 you won't get an error.
The same goes vise-versa. If you are calling a number with a string command it will be an error.
if number == "Hello" then
this will create an error.
So in a scrip it would be like this.
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
string = t[1]
number = t[2]
Now, in-game if you were to type the script:
!words string, 0
It is an error.
because t[1] is already predefined because of param. Making it a string already.
Typing in text is okay and wont create an error. However, t[2] is not
defined as a number. The script reads it as a string. so typing 0, a number, makes an error.
Now if we typed this in game.
!words 0, string
It would be the same. The 0 would create an error, but the string would be fine.
So we need to make the code:
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
string = t[1]
number = tonumber(t[2])
Now if we typed:
!words string, 0
then script would be able to run without an error because the second param (0) is defined as a number.
if we wanted the first param to be defined as a number aswell. you would wright:
string = tonumber(t[1])
number = tonumber(t[2])
and you would be able to say something like this:
!words 1, 2
the script would work.
If you typed:
!words string, string then both would create and error.
So lets say you want to create a script that send a player a couple numbers to add together.
It would look like this.
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
playername.string = getPlayerByNameWildCard(t[1])
number1 = tonumber(t[2])
number2 = tonumber(t[3])
if isPlayer(playername.string) then
if not number1 >= 0 then
doPlayerSendCancel(cid, "You must type a number as the second param.")
return false
end
if not number2 >= 0 then
doPlayerSendCancel(cid, "You must type a number as the third param.")
return fasle
end
doPlayerPopupFYI(playername.string, "Type the value "..number1.." + "..number2)
else
doPlayerSendCancel(cid, "This is not a player.")
return false
end
return true
end
Then in game you would type this:
!math player, 1, 2
the script would give them a message asking what 1 + 2 is.
but if you typed:
!math player, hello, 1
it would send you a cancel telling you that the second param needs to be a number....
If you typed:
!math player, 1, hello
it would send a cancel saying the third param needs to be a number.
I hoped this helped. I plan on adding in more things like this.
Very small things to know but will create the difference in a script working and not working.
The first thing I want to explain:
When using a script to call a value the value must be defined.
What I mean is if the script calls a number. Like this.
if string > 0 then
string = text or letters.
0+ = a number value
If you do not define string as a number the script will give an error and will never work.
in TFS to define strings or values you use these commands.
tonumber(x) -- defines it as a number
tostring(x) -- defines x as a string
So it would be something like this.
value = tonumber(x)
Now if you wright:
value > 0 you won't get an error.
The same goes vise-versa. If you are calling a number with a string command it will be an error.
if number == "Hello" then
this will create an error.
So in a scrip it would be like this.
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
string = t[1]
number = t[2]
Now, in-game if you were to type the script:
!words string, 0
It is an error.
because t[1] is already predefined because of param. Making it a string already.
Typing in text is okay and wont create an error. However, t[2] is not
defined as a number. The script reads it as a string. so typing 0, a number, makes an error.
Now if we typed this in game.
!words 0, string
It would be the same. The 0 would create an error, but the string would be fine.
So we need to make the code:
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
string = t[1]
number = tonumber(t[2])
Now if we typed:
!words string, 0
then script would be able to run without an error because the second param (0) is defined as a number.
if we wanted the first param to be defined as a number aswell. you would wright:
string = tonumber(t[1])
number = tonumber(t[2])
and you would be able to say something like this:
!words 1, 2
the script would work.
If you typed:
!words string, string then both would create and error.
So lets say you want to create a script that send a player a couple numbers to add together.
It would look like this.
function onSay(cid, words, param, channel)
if param == "" then
doPlayerSendCancel(cid, "Command requires param.")
return true
end
t = string.explode(param, ",")
playername.string = getPlayerByNameWildCard(t[1])
number1 = tonumber(t[2])
number2 = tonumber(t[3])
if isPlayer(playername.string) then
if not number1 >= 0 then
doPlayerSendCancel(cid, "You must type a number as the second param.")
return false
end
if not number2 >= 0 then
doPlayerSendCancel(cid, "You must type a number as the third param.")
return fasle
end
doPlayerPopupFYI(playername.string, "Type the value "..number1.." + "..number2)
else
doPlayerSendCancel(cid, "This is not a player.")
return false
end
return true
end
Then in game you would type this:
!math player, 1, 2
the script would give them a message asking what 1 + 2 is.
but if you typed:
!math player, hello, 1
it would send you a cancel telling you that the second param needs to be a number....
If you typed:
!math player, 1, hello
it would send a cancel saying the third param needs to be a number.
I hoped this helped. I plan on adding in more things like this.
Very small things to know but will create the difference in a script working and not working.
Last edited: