• Trying to set a var when you set something
    4 replies, posted
I try: [CODE] function SaySomethingToChange(text, ply, public) text = self.AttackMode end hook.Add('PlayerSay', 'IfSaySomething', SaySomethingToChange) [/CODE] And it gives me a error: [QUOTE] attempt to index global 'self' (a nil value) [/QUOTE]
Well if you read the error, you'd know the problem. As the error suggests, in your code, the variable "self" is nil, i.e. it is undefined, it means nothing. As such, you cannot access the variable as it is nothing. It's like trying to take of wheels (AttackMode) of a non existent imaginary car (self) and put the wheels onto a real car (text), you can't do it because the first car never existed to begin with.
Robotboy I am new at coding so you cant expect me to be good at fixing errors that easy. I noticed it said self is a nil value and I looked over it, I am making a swep and I know self basiclly means the swep. So that's probably not even the problem since I used self.whatever everywhere else in my script. [editline]16th July 2016[/editline] + I tried (text = self.AttackMode) and (self.AttackMode = text)
[QUOTE=GoldenLeopard;50719977]Robotboy I am new at coding so you cant expect me to be good at fixing errors that easy. I noticed it said self is a nil value and I looked over it, I am making a swep and I know self basiclly means the swep. So that's probably not even the problem since I used self.whatever everywhere else in my script. [editline]16th July 2016[/editline] + I tried (text = self.AttackMode) and (self.AttackMode = text)[/QUOTE] I understand that you are new and I am sorry if I come off hostile or offensive or anything, but I'd expect you to at least try to research the subject you are learning before asking for help. That being said, the reason self is nil is because as I said, it is not defined in your code. By default self will only work in any function of this format: [code] function SOMETHING:FunctionName( blah, blh ) print( self ) -- ^ notice the colon in the function name, self will work here end [/code] In your code, as you are clearly not working inside a SWEP[b]:[/b]functionname function, self will be nil. self is not a magical variable that will always "basically mean SWEP". Logic applies to programming.
[QUOTE=GoldenLeopard;50719977]Robotboy I am new at coding so you cant expect me to be good at fixing errors that easy. I noticed it said self is a nil value and I looked over it, I am making a swep and I know self basiclly means the swep. So that's probably not even the problem since I used self.whatever everywhere else in my script. [editline]16th July 2016[/editline] + I tried (text = self.AttackMode) and (self.AttackMode = text)[/QUOTE] It only worked everywhere else in your script because self refers to an object. You can't just create a function and expect self to work on it because it's not an object. Unless you put something like SWEP: or ENT: in front of the function name. ninja'd
Sorry, you need to Log In to post a reply to this thread.