Jump to content

Need a little help...


Xierra

Recommended Posts

Is there an error with this codes?

I`m practicing a bit for new resources made by myself.

function Weaponskill1 () --I mean 1 is pistol, 2 is silenced, etc
local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69
if (pistolstat < 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")
if (pistolstat > 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")
if (pistolstat = 1000 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")
else
outputChatBox ("It made an error, try again or remake it")
end
end
end
addcommandhandler ("pistolskill", Weaponskill1)

*Edit*

Looks much better! Thanks Nich. :wink:

*End*

How to make for example:

If my pistol skill is 0-500, my pistol skill is bad

if my pistol skill is 500-800, my pistol skill is good

if my pistol skill is 800-999, my pistol skill is great

if my pistol skill is 1000, my pistol skill is Hitman(dual pistols).

Do you know how to do it? Any help is appreciated ;)

Thanks

And also if you can, what about making result on the chat box like this:

Pistol Skill: 673, good

Silenced Pistol Skill: 801, great

Desert Eagle Skill: 1000, Hitman

Shotgun Skill: 465, Bad

Sawnoff Shotgun Skill: 1000, Hitman

SPAS12 Shotgun Skill: 768, good

Micro Uzi Skill: 950, great

MP5 Skill: 1000, Hitman

AK47 Skill: 874, great

M5 Skill: 1000, Hitman

Sniper Rifle Skill: 1000, Hitman

Do you know how to do this one?

Edited by Guest
Link to comment

Is there an error with this codes?

I`m practicing a bit for new resources made by myself.

function Weaponskill1 () --I mean 1 is pistol, 2 is silenced, etc
local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69
if (pistolstat < 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")
if (pistolstat > 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")
if (pistolstat = 1000 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")
else
outputChatBox ("It made an error, try again or remake it")
end
end
end
addcommandhandler ("pistolskill", Weaponskill1)

*Edit*

Looks much better! Thanks Nich. :wink:

*End*

How to make for example:

If my pistol skill is 0-500, my pistol skill is bad

if my pistol skill is 500-800, my pistol skill is good

if my pistol skill is 800-999, my pistol skill is great

if my pistol skill is 1000, my pistol skill is Hitman(dual pistols).

Do you know how to do it? Any help is appreciated ;)

Thanks

And also if you can, what about making result on the chat box like this:

Pistol Skill: 673, good

Silenced Pistol Skill: 801, great

Desert Eagle Skill: 1000, Hitman

Shotgun Skill: 465, Bad

Sawnoff Shotgun Skill: 1000, Hitman

SPAS12 Shotgun Skill: 768, good

Micro Uzi Skill: 950, great

MP5 Skill: 1000, Hitman

AK47 Skill: 874, great

M5 Skill: 1000, Hitman

Sniper Rifle Skill: 1000, Hitman

Do you know how to do this one?

Edited by Guest
Link to comment

* NOTE: Big post alert *

There are a few small problems in your code straight off.

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69
if (pistolstat < 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")
if (pistolstat > 500 ) then 
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")
if (pistolstat = 1000 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")
else
outputChatBox ("It made an error, try again or remake it")
end

It might seem problemless right now. I'll change the indentation to show you the problem:

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69
if (pistolstat < 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")
if (pistolstat > 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")
if (pistolstat = 1000 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")
else
outputChatBox ("It made an error, try again or remake it")
end

As I typed above is how the MTA server will most likely read your script file. The problem is that you won't get any output at all (if even work without outputting an error, which it won't because you're missing 2 ends) unless you have a skill level of under 500, in which case it will only display "Your Pistol weapon skill is bad". It might be a little tricky for you to understand, but in this state your MTA server won't be able to understand the code at all.

The solution is "elseif", which will make it more clear to Lua what to do. Like this:

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69
if (pistolstat < 500 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")
elseif (pistolstat >= 500 ) then -- elseif included, which makes sure it will actually work. (also did >= instead of >, so it will also execute the function below if the stat equals to 500)
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")
elseif (pistolstat = 1000 ) then
outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")
else -- If neither of the above (impossible)
outputChatBox ("It made an error, try again or remake it")
end

In this way, the server will understand how you want it to respond. Now we'll head over to a slightly smaller problem:

else -- If neither of the above (impossible)
outputChatBox ("It made an error, try again or remake it")
end

As you can see, I put "impossible" there. Why you might think? Let's see into all the things getPedStat can return:

- 0 ... 1000 (depending on skill level)

- false (in case of an error)

It might seem you might get "it made an error..." output when it fails, but actually you won't. The culprit?

if (pistolstat < 500 ) then

If pistolstat is false, you're trying to compare a boolean with an int; which is illegal, and therefore returns an error and stops the script for a moment. This causes the last error to never appear.

To solve this, we can move this check upwards. Meaning that you'll see if there's an illegal return value before Lua itself has to notice it. How you might ask?

if (pistolstat == false) then -- If pistolstat is equal to false, then...

OR

if (not pistolstat) then -- If pistolstat is an illegal return value (false or nil)

OR

if (type(pistolstat)~="number") then -- If pistolstat isn't a number (therefore something you don't want)

Which way you will choose shouldn't matter in your case. All that matters is the right implementation.

I'll let you try the next step yourself. Try to use your knowledge on "elseif" to place your error check BEFORE pistolstat < 500.

------------------------------------------------------------------------

And also if you can, what about making result on the chat box like this:

Pistol Skill: 673, good

Silenced Pistol Skill: 801, great

This is certainly possible. Note that you can concatenate strings and numbers into a string. So like:

string1 = "Hello"
string2 = "World!"
int1 = 1
int2 = 2
 
outputChatBox(string1 .. " " .. string2) -- Outputs: "Hello World!"
outputChatBox(int1 .. " + " .. int2 .. " = " .. int1 + int2) -- Outputs: "1 + 2 = 3"

Try to make something with this knowledge, and reply if you've got a problem you can't solve. ;)

Link to comment

* NOTE: Big post alert *

There are a few small problems in your code straight off.

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69if (pistolstat < 500 ) then    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")if (pistolstat > 500 ) then     outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")if (pistolstat = 1000 ) then    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")else    outputChatBox ("It made an error, try again or remake it")end

It might seem problemless right now. I'll change the indentation to show you the problem:

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69if (pistolstat < 500 ) then    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")    if (pistolstat > 500 ) then        outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")        if (pistolstat = 1000 ) then            outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")        else            outputChatBox ("It made an error, try again or remake it")        end

As I typed above is how the MTA server will most likely read your script file. The problem is that you won't get any output at all (if even work without outputting an error, which it won't because you're missing 2 ends) unless you have a skill level of under 500, in which case it will only display "Your Pistol weapon skill is bad". It might be a little tricky for you to understand, but in this state your MTA server won't be able to understand the code at all.

The solution is "elseif", which will make it more clear to Lua what to do. Like this:

local pistolstat = getPedStat (source, 69 ) --The Pistol Skill is ID 69if (pistolstat < 500 ) then    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is bad.")elseif (pistolstat >= 500 ) then -- elseif included, which makes sure it will actually work. (also did >= instead of >, so it will also execute the function below if the stat equals to 500)    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is good.")elseif (pistolstat = 1000 ) then    outputChatBox ( getPlayerName (source) .. "Your Pistol weapon skill is Hitman(dual pistols)!")else -- If neither of the above (impossible)    outputChatBox ("It made an error, try again or remake it")end

In this way, the server will understand how you want it to respond. Now we'll head over to a slightly smaller problem:

else -- If neither of the above (impossible)    outputChatBox ("It made an error, try again or remake it")end

As you can see, I put "impossible" there. Why you might think? Let's see into all the things getPedStat can return:

- 0 ... 1000 (depending on skill level)

- false (in case of an error)

It might seem you might get "it made an error..." output when it fails, but actually you won't. The culprit?

if (pistolstat < 500 ) then

If pistolstat is false, you're trying to compare a boolean with an int; which is illegal, and therefore returns an error and stops the script for a moment. This causes the last error to never appear.

To solve this, we can move this check upwards. Meaning that you'll see if there's an illegal return value before Lua itself has to notice it. How you might ask?

if (pistolstat == false) then -- If pistolstat is equal to false, then...

OR

if (not pistolstat) then -- If pistolstat is an illegal return value (false or nil)

OR

if (type(pistolstat)~="number") then -- If pistolstat isn't a number (therefore something you don't want)

Which way you will choose shouldn't matter in your case. All that matters is the right implementation.

I'll let you try the next step yourself. Try to use your knowledge on "elseif" to place your error check BEFORE pistolstat < 500.

------------------------------------------------------------------------

And also if you can, what about making result on the chat box like this:

Pistol Skill: 673, good

Silenced Pistol Skill: 801, great

This is certainly possible. Note that you can concatenate strings and numbers into a string. So like:

string1 = "Hello"string2 = "World!"int1 = 1int2 = 2 outputChatBox(string1 .. " " .. string2) -- Outputs: "Hello World!"outputChatBox(int1 .. " + " .. int2 .. " = " .. int1 + int2) -- Outputs: "1 + 2 = 3"

Try to make something with this knowledge, and reply if you've got a problem you can't solve. ;)

Link to comment

And you'll have to put the == 1000 upper than >= 500, because 1000 is already included in this line >= 500 and it makes the last elseif pointless.

Or just like this:

elseif pistolstat >= 500 and pistolstat <1000 then

LUA highlighter's bbcode is [lua] [./lua] without the dot(.)

Link to comment

And you'll have to put the == 1000 upper than >= 500, because 1000 is already included in this line >= 500 and it makes the last elseif pointless.

Or just like this:

elseif pistolstat >= 500 and pistolstat <1000 then

LUA highlighter's bbcode is [lua] [./lua] without the dot(.)

Link to comment
It might seem you might get "it made an error..." output when it fails, but actually you won't. The culprit?
if (pistolstat < 500 ) then

 

If pistolstat is false, you're trying to compare a boolean with an int; which is illegal, and therefore returns an error and stops the script for a moment. This causes the last error to never appear.

 

To solve this, we can move this check upwards. Meaning that you'll see if there's an illegal return value before Lua itself has to notice it. How you might ask?

 

if (pistolstat == false) then -- If pistolstat is equal to false, then...

OR

if (not pistolstat) then -- If pistolstat is an illegal return value (false or nil)

OR

if (type(pistolstat)~="number") then -- If pistolstat isn't a number (therefore something you don't want)

@Gamesnert : "Where should I put these?"

@Nich: "Oh, so there is "and", thanks so much"

I Haven't tried yet. Because I don't have much time and I have to wait until friday to play! My Parents' rules...

Link to comment
It might seem you might get "it made an error..." output when it fails, but actually you won't. The culprit?
if (pistolstat < 500 ) then

 

If pistolstat is false, you're trying to compare a boolean with an int; which is illegal, and therefore returns an error and stops the script for a moment. This causes the last error to never appear.

 

To solve this, we can move this check upwards. Meaning that you'll see if there's an illegal return value before Lua itself has to notice it. How you might ask?

 

if (pistolstat == false) then -- If pistolstat is equal to false, then...

OR

if (not pistolstat) then -- If pistolstat is an illegal return value (false or nil)

OR

if (type(pistolstat)~="number") then -- If pistolstat isn't a number (therefore something you don't want)

@Gamesnert : "Where should I put these?"

@Nich: "Oh, so there is "and", thanks so much"

I Haven't tried yet. Because I don't have much time and I have to wait until friday to play! My Parents' rules...

Link to comment
if (not pistolstat) then
   ...
elseif (pistolstat < 500 ) then
   ...
...
end

I haven't edited the original code itself, but this is just to give you an idea of where your errordetection should be. (I hope :P )

In simple words: In front of your "if pistolstat < 500" check. As that's the place where the script would otherwise abort due to comparing an int with a boolean.

Link to comment

if (not pistolstat) then    ...elseif (pistolstat < 500 ) then    ......end

I haven't edited the original code itself, but this is just to give you an idea of where your errordetection should be. (I hope :P )

In simple words: In front of your "if pistolstat < 500" check. As that's the place where the script would otherwise abort due to comparing an int with a boolean.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...