View unanswered posts | View active topics It is currently Wed Oct 23, 2019 3:57 am

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Expected "=" near "end"
Author Message

Joined: Mon Jun 28, 2010 10:06 pm
Posts: 461
Location: STEPPING INTO THE RING
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.
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
Data Realms Elite

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
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

Joined: Mon Jun 28, 2010 10:06 pm
Posts: 461
Location: STEPPING INTO THE RING
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
REAL AMERICAN HERO

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
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
Data Realms Elite

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
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.

Sun Aug 07, 2011 2:48 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 5 posts ]

#### Who is online

Users browsing this forum: No registered users

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

Search for:
 Jump to:  Select a forum ------------------ Cortex Command    Dev Log    Wiki    Game Discussion    Game and Licensing Support    Mod Making       Lua Scripting       Requests    Mod Releases       Older Mods    Scene Releases       Scene Making Planetoid Pioneers    Game Discussion    Pioneer Program Lounge Data Realms Off-Topic    Off Topic Chat       Gaming       Forum Games    Site Support & Feedback