• Partially Random Deathmessage
    4 replies, posted
Hi, I'm very new to GLua, and programming in general. I'm trying to have random death messages in chat, when somebody is killed, the problem I am having is that I have no idea how to have the random part appear. Here is the death message part of my init.lua [CODE]function GM:PlayerDeath( ply, inflictor, attacker ) if ( ply:SteamID() == attacker:SteamID() ) then PrintMessage( HUD_PRINTTALK, ply:Name().. " just could not take the pressure" ) else PrintMessage( HUD_PRINTTALK, ply:Name().. deathMessage, attacker:Name().. " to death" ) end end function RMessageTimer() local RNumber = math.random( 1, 5 ) if RNumber == 1 then deathMessage = " bruttaly raped " if RNumber == 2 then deathMessage = " ate " if RNumber == 3 then deathMessage = " pwned " if RNumber == 4 then deathMessage = " slayed " if RNumber == 5 then deathMessage = " killed " end end end end end end[/CODE] Help would be appericated. Thank you.
Read up on if statements, as you're using them completely wrong. [lua] if RNumber == 1 then deathMessage = " bruttaly raped " elseif RNumber == 2 then deathMessage = " ate " elseif RNumber == 3 then deathMessage = " pwned " elseif RNumber == 4 then deathMessage = " slayed " elseif RNumber == 5 then deathMessage = " killed " end [/lua] Also, try to experiment with tables when you get a chance. They do wonders. You can try this code, it's untested but it should work. [lua] local deathMessages = {"brutally raped", "ate", "pwned", "slayed", "killed"} function RMessageTimer() local RNumber = math.random(1, 5) deathMessage = deathMessages[RNumber] end function GM:PlayerDeath( ply, inflictor, attacker ) if ( ply:SteamID() == attacker:SteamID() ) then PrintMessage( HUD_PRINTTALK, ply:Name().. " just could not take the pressure" ) else PrintMessage( HUD_PRINTTALK, ply:Name().. deathMessage, attacker:Name().. " to death" ) end end [/lua]
[QUOTE=wakeboarderCWB;42735686]Read up on if statements, as you're using them completely wrong. [lua] if RNumber == 1 then deathMessage = " bruttaly raped " elseif RNumber == 2 then deathMessage = " ate " elseif RNumber == 3 then deathMessage = " pwned " elseif RNumber == 4 then deathMessage = " slayed " elseif RNumber == 5 then deathMessage = " killed " end [/lua] Also, try to experiment with tables when you get a chance. They do wonders. You can try this code, it's untested but it should work. [lua] local deathMessages = {"brutally raped", "ate", "pwned", "slayed", "killed"} function RMessageTimer() local RNumber = math.random(1, 5) deathMessage = deathMessages[RNumber] end function GM:PlayerDeath( ply, inflictor, attacker ) if ( ply:SteamID() == attacker:SteamID() ) then PrintMessage( HUD_PRINTTALK, ply:Name().. " just could not take the pressure" ) else PrintMessage( HUD_PRINTTALK, ply:Name().. deathMessage, attacker:Name().. " to death" ) end end [/lua][/QUOTE] Thanks a bunch! I really appericate your help.
I guess just using table.random would fit this better?
[QUOTE=LennyPenny;42735844]I guess just using table.random would fit this better?[/QUOTE] This seems like the way to go, thank you for teaching me master.
Sorry, you need to Log In to post a reply to this thread.