LUA Help

Status
Not open for further replies.

MudkipYeah!

Vastly Short Attention Span
Welp, I knew I was going to have to hit this place up eventually. Here is the code that is acting up.
Code:
local MachSpeed = false
local CurrentSpeed = player.mo.speed
local PlayerMachSpeed = player.mo.normalspeed - 5
AddHook ("ThinkFrame", do
	for player in players.iterate
		if player.mo then --Is player there?
			if CurrentSpeed > PlayerMachSpeed then --Is speed above the max speed minus 5?
			MachSpeed = true --Set value true
			if CurrentSpeed < PlayerMachSpeed then --Is speed below the max speed minus 5?
			MachSpeed = false --Set value false
			if CurrentSpeed == PlayerMachSpeed then --Is speed equal to the max speed minus 5?
			PF_THOK --Make damageable 
		end
	end
end
	if player.mo and --Is player there?
		if MachSpeed = true --Is value true?
		then P_ElementalFireTrail(player) --Spawn a fire trail
		and PF_GODMODE --Make invincible
		and local print("88 MPH!") --Print out reference
		and if P_CheckMeleeRange(actor) = true --Is player in hugging distance?
		then P_DoPlayerPain(player, source, inflictor) --Knock 'em out!
		and PF_THOK --Make damageable
		end
	end	
end)
end
end
Line 13: Expected '=' near 'end'.
Any idea what's causing this? Feel free to bash me for the syntax and such, but I'm just having fun with this. This error is produced by just adding the lua, and it's certainly vague for me. Did I mess up somewhere on the way? Appreciate any help.
 
Just look back to history; You will see that it’s filled with greed

I think you have to use '==' for checking to see if something is equal to something, not just '='.
 
There are so many glaring problems with this code besides that error, I feel it may be easier to highlight all the places that are wrong in particular:

Code:
local MachSpeed = false
local CurrentSpeed = player.mo.speed
local PlayerMachSpeed = player.mo.normalspeed - 5
[COLOR="Lime"]a[/COLOR]ddHook ("ThinkFrame", do
	for player in players.iterate
		if player.mo then --Is player there?
			if CurrentSpeed > PlayerMachSpeed then --Is speed above the max speed minus 5?
				MachSpeed = true --Set value true
			[COLOR="Lime"]else[/COLOR]if CurrentSpeed < PlayerMachSpeed then --Is speed below the max speed minus 5?
				MachSpeed = false --Set value false
			[COLOR="lime"]else[/COLOR]if CurrentSpeed == PlayerMachSpeed then --Is speed equal to the max speed minus 5?
				[COLOR="Red"]PF_THOK[/COLOR] --Make damageable 
		[COLOR="Orange"]end
	end
end[/COLOR]
	if player.mo [COLOR="Orange"]and[/COLOR] --Is player there?
		if MachSpeed =[COLOR="lime"]=[/COLOR] true --Is value true?
			then P_ElementalFireTrail(player) --Spawn a fire trail
			[COLOR="Red"]and PF_GODMODE[/COLOR] --Make invincible
			[COLOR="Red"]and[/COLOR] local print("88 MPH!") --Print out reference
		[COLOR="lime"]end[/COLOR]
		[COLOR="red"]and[/COLOR] if P_CheckMeleeRange(actor) =[COLOR="Lime"]=[/COLOR] true --Is player in hugging distance?
			then P_DoPlayerPain(player, source, inflictor) --Knock 'em out!
			[COLOR="red"]and PF_THOK[/COLOR] --Make damageable
		[COLOR="Lime"]end[/COLOR]
	end
end)
[COLOR="orange"]end
end[/COLOR]

Key:

My corrections/additions
Why on earth are you doing this?? This is clearly wrong
??? (mostly confusion about "end"s)

My attempts to summarise what you've done wrong here:

  • "=" is for changing the value of a variable, not for comparison. "==" is the thing to use instead for that (this is the issue that caused your error)
  • "and" is only meant to be used for if-statements, and is definitely NOT needed to link multiple lines of assignments together
  • if you want to add PF_GODMODE onto the player's pflags, use "player.pflags = $1 | PF_GODMODE", if you wanted to remove such a flag instead use "player.pflags = $1 & ~PF_GODMODE". Same sort of syntax is to be used if you wanted to remove individual flags from any kind of flags-holding variable.
  • What on earth is "PF_THOK"?

Now that I'm done here, I strongly advise you read up on Lua on the SRB2 Wiki (see here) and look through the lua-users Tutorials (see here) before you go on doing Lua scripting.

As a final note, "Lua" is not an acronym. It's Portuguese for "moon". And now I go to sleep, zzzz....
 
Last edited:
Thanks Monster, I really appreciate your help, and um, sorry for having you sit through that awful mess I dared to call code. I'm gonna try to get better at it.
 
I know MI responded as I was typing this up, but I'm not about to let half an hour go to waste so I'd like to try to explain why the things that are wrong as wrong. Bear with me here, because I'm about to dump a ton of comments on this script and it'll probably be hard to understand unless you reread everything a couple of times and ask questions about what you don't understand. I'm going to add line numbers to make explaining the problems easier.
Code:
 1  local MachSpeed = false
 2  local CurrentSpeed = player.mo.speed
 3  local PlayerMachSpeed = player.mo.normalspeed - 5
 4  AddHook ("ThinkFrame", do
 5      for player in players.iterate
 6          if player.mo then --Is player there?
 7              if CurrentSpeed > PlayerMachSpeed then --Is speed above the max speed minus 5?
 8              MachSpeed = true --Set value true
 9              if CurrentSpeed < PlayerMachSpeed then --Is speed below the max speed minus 5?
10              MachSpeed = false --Set value false
11              if CurrentSpeed == PlayerMachSpeed then --Is speed equal to the max speed minus 5?
12              PF_THOK --Make damageable 
13          end
14      end
15  end
16      if player.mo and --Is player there?
17          if MachSpeed = true --Is value true?
18          then P_ElementalFireTrail(player) --Spawn a fire trail
19          and PF_GODMODE --Make invincible
20          and local print("88 MPH!") --Print out reference
21          and if P_CheckMeleeRange(actor) = true --Is player in hugging distance?
22          then P_DoPlayerPain(player, source, inflictor) --Knock 'em out!
23          and PF_THOK --Make damageable
24          end
25      end    
26  end)
27  end
28  end

Line 1: This technically runs, but you won't be able to support multiple players with this. My suggestion would be to remove this line, and replace any references to MachSpeed with player.mo.MachSpeed.

Lines 2-3: player does not exist yet. You'll get an error trying to access a property of nil. I'd move these inside the ThinkFrame hook's function.

Line 3: player.mo.normalspeed isn't a valid property. Are you thinking of mo.normalspeed? Also, subtracting 5 from a value like mo.normalspeed is the equivalent of subtracting 0.00009 from a floating-point value that would represent similar. (Floating-point values don't exist in SRB2) If you want to subtract five units from the value, you'd subtract 5*FRACUNIT.

Line 4: Lua is case sensitive; AddHook is undefined by default. You want to use addHook. That is, make the A lowercase.

Lines 7, 9 and 11: These if statements aren't closed properly. Every if block needs a matching end. You can either add one after the following statements for each of these, or, since these statements are all mutually exclusive, change the latter two ifs to elseifs to connect them together, and only add the end after the final statement in the whole block. Something like this:

Lines 8, 10, and 12: This is a minor nitpick, but for readability you should always indent one level farther for each nested if/for/etc block.

Line 12: PF_THOK is not a valid statement and makes no sense. If you're trying to set the thokked flag, the statement would look something like player.pflags = $1 | PF_THOKKED, but that wouldn't do what the comment is suggesting. In fact, the conditional right above it (on line 12) is nearly impossible to achieve, since you're asking for a fixed-point value to have an exact match, which means it'll return false on even a 0.000015 difference. The best way to handle making the player undamagable conditionally would be to use a ShouldDamage hook and check the conditions desired.

Line 15: Assuming you added an end to close the if blocks above, you're now left with one too many end statements in this chain. The one right here would close out the entire function, which is unintended judging by the code after this.

Line 16: You've left the for player in players.iterate loop by this point, so player is no longer defined. You'll be accessing a nil value here. However, before you do anything, scroll down to my comments on lines 18-23. (Skip the line 17 comment for now)

Line 17: The if immediately following an and will throw off the interpreter and should be removed. Additionally, = is an assignment operator, not a comparison operator; in other languages this would cause MachSpeed to be set to true and the condition to always pass, but in Lua you get a syntax error. I believe you know about ==, but you could also simply remove the == true part of this statement, since having a conditional statement consist solely of a variable name will pass the condition if the variable is truthy (anything that isn't false, nil or (in SRB2's Lua) 0), and fail if it isn't. Finally, I'd recommend moving the then from the following line up here, for the sake of readability.

Lines 18-23: Considering the conditional checks surrounding this block are also satisfied with the checks on lines 6 and 7, and the latter have no extra conditions that would cause changed behavior, you can move all of these lines to right after line 8 and save some redundant checking. You'd then be left with an empty if statement that you can comment out.

Lines 19-21, and 23: The ands here are syntactically wrong. and is supposed to be used exclusively for comparisons, not to join statements. Simply removing them will fix the issues.

Line 19: Again, a variable by itself is not a valid statement. What you're looking for here is player.pflags = $1 | PF_GODMODE.

20: local should only be used immediately preceding function/variable declarations. Calling print is neither of those, so simply remove the local from this line.

21-23: ??? I'm highly confused by what these are supposed to accomplish. I can tell you they're syntactically incorrect, but no more than that.

27-28: These ends are extraneous and will cause syntax errors. Just remove them.
 
Figured I would miss some of the issues, since I was making my post past midnight in my timezone x_x

Most I could gather about lines 21 to 23 was that they're yet another if statement followed by a function call and a failed attempt at setting the mythical "PF_THOK". Only with misusage of "and" to muddle things up again.

Another thing that should be stressed is your slightly incorrect usage of tab spaces (or just simply "indenting"): you should be using it to make it clear what is part of an if-statement and what is not. It's beneficial for you as well as other people, particularly if you're looking back at your script in the future. I omitted to mention I made an effort to fix this particular issue in my post last night, as part of my attempt to understand what's going on in this Lua script. Just so you know.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top