Did i code these if then else statements right?

I’ve been messing around with game modes lately, and i have changed this, but I wondered if I did this function correctly, did I, and if i didn’t, tell me where I have goofed please.



function GM:DoPlayerDeath( ply, attacker, dmginfo )

	ply:CreateRagdoll()
	
	ply:AddDeaths( 1 )
	
	if (ply:Team() == 1 && attacker:Team() == 2) then
	Msg( "Player " .. ply:GetName() .. " has died at the hands of the Temaplar" .. attacker:GetName() .. "!" )
		else 
			if (ply:Team() == 2 && attacker:Team() == 1) then
				Msg( "Player " .. ply:GetName() .. " has died at the hands of the Assassin" .. attacker:GetName() .. "!" )
					else
						Msg("Player" .. ply:GetName() .. " has died!")
			end
	end
end


Oh and team 1 is the assassins and team 2 is the templars, if this was required. :downs:

[lua]function GM:DoPlayerDeath( ply, attacker, dmginfo )

ply:CreateRagdoll()

ply:AddDeaths( 1 )

if (ply:Team() == 1 && attacker:Team() == 2) then
Msg( "Player " .. ply:GetName() .. " has died at the hands of the Templar " .. attacker:GetName() .. "!" ) 
	elseif (ply:Team() == 2 && attacker:Team() == 1) then
		Msg( "Player " .. ply:GetName() .. " has died at the hands of the Assassin" .. attacker:GetName() .. "!" )
				else
					Msg("Player " .. ply:GetName() .. " has died!")
		end
end

end[/lua]

I think I found everything…

also, use [noparse][lua][/lua][/noparse] tags

[editline]25th October 2010[/editline]

Oh, and for example: if ( thing == 1 ) then, those aren’t required, C++ coders use them in C++

it wont affect your code at all, just saying you don’t need to do extra work

Thanks, so it’s like Visual basic if-thens then? (if you have used it)

I haven’t really used the C++ part of it, I find it easier to just not use them because I tend to do () then press the left arrow key then do my code instead of (, do code, then ).

Also, fix your indentation, it’s not very readable and you’ll have more trouble figuring out what’s wrong.

[lua]function GM:DoPlayerDeath( ply, attacker, dmginfo )

ply:CreateRagdoll()

ply:AddDeaths( 1 )

if ply:Team() == 1 and attacker:Team() == 2 then
	Msg( "Player " .. ply:GetName() .. " has died at the hands of the Temaplar" .. attacker:GetName() .. "!" )
elseif ply:Team() == 2 and attacker:Team() == 1 then
	Msg( "Player " .. ply:GetName() .. " has died at the hands of the Assassin" .. attacker:GetName() .. "!" )
else
	Msg("Player" .. ply:GetName() .. " has died!")
end

end[/lua]

Also, I think you should use “not”, “and”, “or” instead of “!”, “&&”, “||”. Those are not native Lua, Garry added them, so if you ever get to code in Lua for something else than Garry’s Mod, and use shit like &&, you’ll have quite a bad surprise.

But also if you code something in C++, it’ll look less foreign to you.

On second thought, it’s more likely he’ll ever code something in lua than C++. But it’s not that hard to just do a replace all on &&, ||, etc.