Data Realms Fan Forums
http://forums.datarealms.com/

Expected "=" near "end"
http://forums.datarealms.com/viewtopic.php?f=73&t=25002
Page 1 of 1

Author:  Darkdoom [ Sat Aug 06, 2011 11:17 pm ]
Post subject:  Expected "=" near "end"

so I have been dabbling in lua... again... and tried to make a simple script.
Basically, this script should:
1. see is W is being held
2. if W is being held, do some random math
3. make x = whatever the random number is
4. check the number, if x == 100 then
5. while W is held, gib the actor(or object) it is connected to
6. if it does NOT = 100, print a message telling me what it was.

Now I know the likelyhood of a random number being 100 is very, very slim. But that is not the point, the point is to get anything to happen at all.
I am rather sure I am going about this all wrong.
here is the code:
Code:
if UInputMan:KeyHeld(23) then
   x = math.random
   print(x)
end
function Update(self)
   if x == 100 then
      while UInputMan:KeyHeld(23)
         do self.Gibthis
      end
   end
   elseif x ~= 100 then
      print("The number was " x)
   end
end

I really don't know what the problem is... but like I said, I feel like something is wrong, I just cant tell what it is.

The error associated with it is that it expects a = next to the end at line 9... What do I do to fix this?

Author:  Asklar [ Sat Aug 06, 2011 11:42 pm ]
Post subject:  Re: Expected "=" near "end"

Code:
function Update(self)
if UInputMan:KeyHeld(23) then
 x = math.random(100)
  if x == 100 then
   self:GibThis()
  else
   print("The number was ".. x)
   end
end
end


Try this, it should work.

You had many syntax errors, and the first bit of code wasn't in any function, you separated the elseif from the if with an extra end.

Author:  Darkdoom [ Sat Aug 06, 2011 11:47 pm ]
Post subject:  Re: Expected "=" near "end"

ok good that works, mind explaining why it works?
Why wouldn't it have worked at all with x = math.random(100) outside of the function? It would have run anyway, right?

Author:  Grif [ Sun Aug 07, 2011 1:05 am ]
Post subject:  Re: Expected "=" near "end"

Alright so let's see here.

Anything not inside function Update(self) will not be run repeatedly. Meaning generating a random number there will do nothing. You're generating a pointless global variable with an incredibly common name, meaning it's very likely to conflict with other scripts.

While loops are completely unecessary for this application, because everything within Update is run per-frame. There's no need to check more often than that, which is the only purpose of a while loop.

As Asklar fixed, math.random will return a value from 0 to 1; you need to specify boundaries if you want it to work with larger numbers.

Also, as Asklar noted, you closed an if with an end, and then added an elseif. Properly tabbed, the only things visible on the same tab line should be:
if
elseif
else
end

If/elseif/else are all closed by the operator to start the next check/loop. Ends are final, and should only be used at the end of your loop.

Asklar's code is good, with one minor exception. Replace:
Code:
x = math.random(100)


with this:
Code:
local x = math.random(100)


Also, semicolons are optional but neater.

Author:  Asklar [ Sun Aug 07, 2011 2:48 am ]
Post subject:  Re: Expected "=" near "end"

Yeah, I was thinking on puting self.x instead of x, but I thought he was going to use it that way.

Anyway, glad to help.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/