View unanswered posts | View active topics It is currently Thu Mar 28, 2024 8:35 pm



Reply to topic  [ 5 posts ] 
 Expected "=" near "end" 
Author Message
User avatar

Joined: Mon Jun 28, 2010 10:06 pm
Posts: 461
Location: STEPPING INTO THE RING
Reply with quote
Post 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?


Sat Aug 06, 2011 11:17 pm
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
Location: In your office, earning your salary.
Reply with quote
Post 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.


Sat Aug 06, 2011 11:42 pm
Profile
User avatar

Joined: Mon Jun 28, 2010 10:06 pm
Posts: 461
Location: STEPPING INTO THE RING
Reply with quote
Post 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?


Sat Aug 06, 2011 11:47 pm
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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.


Sun Aug 07, 2011 1:05 am
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
Location: In your office, earning your salary.
Reply with quote
Post 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.


Sun Aug 07, 2011 2:48 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.045s | 16 Queries | GZIP : Off ]