thund3rbird23

Why I got nil for this?

Recommended Posts

dbQuery(function(qh) 
                local poll = dbPoll(qh,0)
                for _,v in ipairs(poll) do
      	        local ak47 = v["ak47"]
	 end
                owner = 1
end,{},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

setPedStat(player, 77, ak47)

Bad argument @ 'setPedStat' [Expected number at argument 3, got nil]

Share this post


Link to post
29 minutes ago, thund3rbird23 said:

Bad argument @ 'setPedStat' [Expected number at argument 3, got nil]

 

setPedStat(player, 77, *he want here number* )

delate ak47

 

Weapon skills:
Note: see Weapon skill levels for the values that advance weapon skills.

69: WEAPONTYPE_PISTOL_SKILL

70: WEAPONTYPE_PISTOL_SILENCED_SKILL

71: WEAPONTYPE_DESERT_EAGLE_SKILL

72: WEAPONTYPE_SHOTGUN_SKILL

73: WEAPONTYPE_SAWNOFF_SHOTGUN_SKILL

74: WEAPONTYPE_SPAS12_SHOTGUN_SKILL

75: WEAPONTYPE_MICRO_UZI_SKILL

76: WEAPONTYPE_MP5_SKILL

77: WEAPONTYPE_AK47_SKILL

78: WEAPONTYPE_M4_SKILL

79: WEAPONTYPE_SNIPERRIFLE_SKILL

80: SEX_APPEAL_CLOTHES

81: GAMBLING

 

and put number 77

Share this post


Link to post
5 hours ago, salh said:

 


setPedStat(player, 77, *he want here number* )

delate ak47

 

Weapon skills:
Note: see Weapon skill levels for the values that advance weapon skills.

69: WEAPONTYPE_PISTOL_SKILL

70: WEAPONTYPE_PISTOL_SILENCED_SKILL

71: WEAPONTYPE_DESERT_EAGLE_SKILL

72: WEAPONTYPE_SHOTGUN_SKILL

73: WEAPONTYPE_SAWNOFF_SHOTGUN_SKILL

74: WEAPONTYPE_SPAS12_SHOTGUN_SKILL

75: WEAPONTYPE_MICRO_UZI_SKILL

76: WEAPONTYPE_MP5_SKILL

77: WEAPONTYPE_AK47_SKILL

78: WEAPONTYPE_M4_SKILL

79: WEAPONTYPE_SNIPERRIFLE_SKILL

80: SEX_APPEAL_CLOTHES

81: GAMBLING

 

and put number 77

https://wiki.multitheftauto.com/wiki/SetPedStat

bool setPedStat ( ped thePed, int stat, float value )

value: the new value of the stat. It must be between 0 and 1000.

So set the value to 77...

I want to get the value from mysql database. It's have a weaponstats table which contains id,owner,ak47 column.

And the ak47 table value is 999 and I do query the ak47 row value "local ak47 = v["ak47"]" that's why I did put the ak47 instead of 999.

 

Edited by thund3rbird23

Share this post


Link to post
dbQuery(function(qh) 
                local poll = dbPoll(qh,0)
                for _,v in ipairs(poll) do
      	        local ak47 = v["ak47"] -- first of all, this is defined locally to this scope only so it doesn't exist outside the anonymous function(qh)
	 end
                owner = 1 -- this definition seems very out of place. It should come before dbQuery as it's used in it's parameters
end,{},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

setPedStat(player, 77, ak47) -- this executes right after dbQuery but before the dbQuery's callback function, hence ak47 is not defined

The solution could look something like this

local owner = 1
dbQuery(
  function(qh) 
    local poll = dbPoll(qh,0)
    for _,v in ipairs(poll) do -- if the poll result only has one row, it would be better to use local ak47 = poll[1]["ak47"] instead of this loop
      local ak47 = v["ak47"]
      setPedStat(player, 77, ak47)
    end
  end,
  {},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

Share this post


Link to post
2 hours ago, Investor said:

dbQuery(function(qh) 
                local poll = dbPoll(qh,0)
                for _,v in ipairs(poll) do
      	        local ak47 = v["ak47"] -- first of all, this is defined locally to this scope only so it doesn't exist outside the anonymous function(qh)
	 end
                owner = 1 -- this definition seems very out of place. It should come before dbQuery as it's used in it's parameters
end,{},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

setPedStat(player, 77, ak47) -- this executes right after dbQuery but before the dbQuery's callback function, hence ak47 is not defined

The solution could look something like this


local owner = 1
dbQuery(
  function(qh) 
    local poll = dbPoll(qh,0)
    for _,v in ipairs(poll) do -- if the poll result only has one row, it would be better to use local ak47 = poll[1]["ak47"] instead of this loop
      local ak47 = v["ak47"]
      setPedStat(player, 77, ak47)
    end
  end,
  {},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

Thanks, now works. But I got another problem.

I want to seperate each skills so I did rows for all skills ak47,m4,pistol etc.... and I want to set all stat when the character is loaded but it's only set the ak stat.

Now the code look this:

            local owner = charID
            dbQuery(function(qh) 
            local poll = dbPoll(qh,0)
            for _,v in ipairs(poll) do
            local ak47 = v["ak47"]
			local m4 = v["m4"]
			local pistol = v["pistol"]
			local silencedpistol = v["silencedpistol"]
			local deagle = v["deagle"]
			local shotgun = v["shotgun"]
			local sawnoff = v["sawnoff"]
			local spas = v["spas"]
			local mp5 = v["mp5"]
			local sniper = v["sniper"]
            setPedStat(player, 77, ak47)
			setPedStat(player, 78, m4)
			setPedStat(player, 69, pistol)
			setPedStat(player, 70, silencedpistol)
			setPedStat(player, 71, deagle)
			setPedStat(player, 72, shotgun)
			setPedStat(player, 73, sawnoff)
			setPedStat(player, 74, spas)
			setPedStat(player, 76, mp5)
			setPedStat(player, 79, sniper)
    end
  end,
{},connection,"SELECT * FROM weaponstats WHERE owner=?",owner)

 

Share this post


Link to post
local skills = {
  --[skillID] = columnName
  [77] = "ak47",
  [78] = "m4",
  [69] = "pistol",
  [70] = "silencedpistol",
  [71] = "deagle",
  [72] = "shotgun",
  [73] = "sawnoff",
  [74] = "spas",
  [76] = "mp5",
  [79] = "sniper",
}
local owner = charID
dbQuery(
  function(qh) 
    local poll = dbPoll(qh,0)
    local row = poll[1]
    if not row then return false end -- don't continue if poll[1] is empty (no rows found in weaponstats for this owner)
    for skillID, columnName in pairs(skills) do -- iterate the table defined above whose key-value pairs are [skill ID] = table column name
      setPedStat(player, skillID, row[columnName])
    end
  end,
  {},connection,"SELECT * FROM weaponstats WHERE owner=? LIMIT 1", owner
)

This is a little cleaner and more maintainable design. If you add more columns in your weaponstats db-table, all you have to do is update the skills table.

  • Thanks 1

Share this post


Link to post
1 hour ago, Investor said:

local skills = {
  --[skillID] = columnName
  [77] = "ak47",
  [78] = "m4",
  [69] = "pistol",
  [70] = "silencedpistol",
  [71] = "deagle",
  [72] = "shotgun",
  [73] = "sawnoff",
  [74] = "spas",
  [76] = "mp5",
  [79] = "sniper",
}
local owner = charID
dbQuery(
  function(qh) 
    local poll = dbPoll(qh,0)
    local row = poll[1]
    if not row then return false end -- don't continue if poll[1] is empty (no rows found in weaponstats for this owner)
    for skillID, columnName in pairs(skills) do -- iterate the table defined above whose key-value pairs are [skill ID] = table column name
      setPedStat(player, skillID, row[columnName])
    end
  end,
  {},connection,"SELECT * FROM weaponstats WHERE owner=? LIMIT 1", owner
)

This is a little cleaner and more maintainable design. If you add more columns in your weaponstats db-table, all you have to do is update the skills table.

Yes, this is way more cleaner, thanks. But I don't understand clearly, sorry. So what I need to do with the table? For example I want to set the deagle stat 999 how will the table look?

id 1, stat 71 owner 1 and where do I put the skill value (999) ? or I dont interpret well?

Share this post


Link to post

The skill value is looked up in the SQL table weaponstats.

Since you're already accounting for all the weapon skills possible, let's hypothetically say you want to add stamina stat to this. What you'd do is add a new column in the weaponstats SQL table called "stamina", and then edit this script's skills table by adding [22] = "stamina". The actual value for that stat will be in the SQL table. E.g.

owner ak47 m4 ... stamina
23 1000 1000 ... 560
35 600 500 ... 600

So for player with charID 23, the poll[1]["stamina"] will be 560, for poll[1]["m4"] it will be 1000. For charID 35, these will be 600 and 500 respectively.

When originally recommending that code, I didn't realise that all the skills were already added, which means you probably won't be adding or removing anything from it, but at least it's still a cleaner code design.

Edited by Investor
  • Like 1

Share this post


Link to post

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.