English why my script hive Error

12 replies
Goto Page
To the start Previous 1 Next To the start
08.08.18 08:08:53 pm
Up
Hazard_h
User
Offline Off
Hi all I have a script lua a mistake that I do not know. Please help me

Code:
1
2
3
LUA ERROR: sys/lua/IF/sys/hooks.lua:337: attempt to index global 'havebal' (a nil value)
 -> sys/lua/IF/sys/hooks.lua:337: in function <sys/lua/IF/sys/hooks.lua:336>
 -> in Lua hook 'drop', params: 2, 358, 74, 0, 0, 0, 52, 13


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function rp_drop(id,iid,type,ain,a,mode,x,y)
     if havebal[id]==true then
          parse("strip "..id.." "..type)
          parse("spawnitem "..type.." "..x.." "..y)
          havebal[id]=false
          return 1
     end
     for k, v in ipairs(Addons) do
          if v.FHave[id]==1 then
               if v.wpn == type then
                    gmsg2(id,"255000000","You can't drop your weapon!")
                    return 1
               end
          end
     end
end
08.08.18 08:12:48 pm
Up
Masea
Super User
Offline Off
Have you declared
havebal
as a table yet?
Create and design your GUI easy and fast: file cs2d GUI Framework (12) | Go deep of the darkness and try your intrepidity: file cs2d Outlast 2 Modification (25)
08.08.18 09:23:50 pm
Up
Hazard_h
User
Offline Off
No I'm not declared table
08.08.18 09:29:14 pm
Up
Masea
Super User
Offline Off
So do that. You cannot declare a variable in a table that not yet exist.

By the way,
if havebal[id] == true then
and
if havebal[id] then
are totally the same statements, use the second one instead to keep your code a bit more clean.
Create and design your GUI easy and fast: file cs2d GUI Framework (12) | Go deep of the darkness and try your intrepidity: file cs2d Outlast 2 Modification (25)
08.08.18 09:47:01 pm
Up
Rainoth
Moderator
Offline Off
@user Masea: Well, technically, one's checking if a variable is true while the other is checking if the variable exists or is true.
08.08.18 10:16:03 pm
Up
Masea
Super User
Offline Off
@user Rainoth: Although, it depends on what you get when one said: "exists".

Code:
x = true

x variable exists and is true and the both statement would be true.

Code:
x = false

x variable exists and is false and the both statement would be false.

Code:
local x

x variable exists and is undefined? The both statement would be false anyway.

As a result, I'd say they both are totally the same, even if they are not when it comes to "technically" saying.

@user Rainoth: I posted this comment just to make sure if you actually meant that you have seen other possibilities that these both statements cannot be matched to each other when you saying that.
Create and design your GUI easy and fast: file cs2d GUI Framework (12) | Go deep of the darkness and try your intrepidity: file cs2d Outlast 2 Modification (25)
08.08.18 10:31:38 pm
Up
Avo
User
Offline Off
1) Declare global
havebal={}


or:

2) If it is some kind of a football script, maybe it should be
haveball
?
08.08.18 10:49:39 pm
Up
Hazard_h
User
Offline Off
Add it in script want is a good Lua ??? @user Avo:
@user Masea: @user Rainoth:
09.08.18 03:38:22 am
Up
Rainoth
Moderator
Offline Off
@user Masea: Yes I had in mind the usability of both. They do work like you mentioned but the one without comparison can also be used to check if variables exist (like say if a variable existed even if it wasn't a boolean).
Meaning although you can use both in this scenario, they are not technically the same because you can use one in a wider variety of cases.

@user Hazard_h: Like we told you before, your 'havebal' variable doesn't exist.

You need to define it like so
Code:
1
haveball = {}

and then set it to true for a player when he's supposed to have it true.
From looking at the code, it seems like it should "simulate" dropping an item while preventing the actual drop of the item. I'm guessing it's to spawn an item with full ammo or something like that.
09.08.18 08:58:31 am
Up
Hazard_h
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function rp_drop(id,iid,type,ain,a,mode,x,y)
    haveball = {}
     if havebal[id]==true then
          parse("strip "..id.." "..type)
          parse("spawnitem "..type.." "..x.." "..y)
          havebal[id]=false
          return 1
     end
     for k, v in ipairs(Addons) do
          if v.FHave[id]==1 then
               if v.wpn == type then
                    gmsg2(id,"255000000","You can't drop your weapon!")
                    return 1
               end
          end
     end
end

@user Rainoth: you mean this ..
09.08.18 11:43:52 am
Up
Avo
User
Offline Off
More like this:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
havebal = {}

function rp_drop(id,iid,type,ain,a,mode,x,y)
     if havebal[id]==true then
          parse("strip "..id.." "..type)
          parse("spawnitem "..type.." "..x.." "..y)
          havebal[id]=false
          return 1
     end
     for k, v in ipairs(Addons) do
          if v.FHave[id]==1 then
               if v.wpn == type then
                    gmsg2(id,"255000000","You can't drop your weapon!")
                    return 1
               end
          end
     end
end

Global means that it is not declared inside any function (like rp_drop).
09.08.18 12:59:51 pm
Up
Hazard_h
User
Offline Off
thx you dude :^) script ts working
09.08.18 10:33:03 pm
Up
Quattro
User
Offline Off
I bet this is the outcome
haveball = {}
for i = 1, 1000000 do
     local id = i
     if havebal[id] then
          msg('will we ever see this message? ;-;')
     end
end
To the start Previous 1 Next To the start