Some of your variables didn't really need to be global, therefor I moved them inside of the function.
Code:
if (g_Killer && is_user_alive(g_Killer))
{
// ...
else
{
g_Kills = 0 // Clear killstreak counter
}
This check is pretty good for a beginner since most beginners just assume that everyone is connected and alive. But in this case I felt that it's more relevant if people are connected than actually alive. Being alive doesn't affect the function that you are trying to achieve. Also, if someone would commit suicide g_Killer would be 0 and therefor you would reset the counter.
Code:
if (g_Kills[killer] >= 5 && g_Kills[killer] < 10) // Give +2 points for each kill if players' killstreak is equal or more than 5
{
set_user_frags(killer, get_user_frags(killer) + 1)
}
else if (g_Kills[killer] >= 10) // Give +3 points for each kill if players' killstreak is equal or more than 10
{
set_user_frags(killer, get_user_frags(killer) + 2)
}
If you want you could replace this with
Code:
set_user_frags(killer, get_user_frags(killer) + g_Kills[killer] >= 10 ? 2 : g_Kills[killer] >= 5 ? 1 : 0)
This will make the code slightly harder to read but much more compact.
You could also do this but then it wouldn't stop at +2, it could continue forever.
Code:
set_user_frags(killer, get_user_frags(killer) + g_Kills[killer] / 5)
You could of course solve that by doing this which is compact and readable.
Code:
set_user_frags(killer, get_user_frags(killer) + clamp(g_Kills[killer] / 5, 0, 2))
The clamp(value, min, max) makes sure that
value stays between
min and
max
In the end coding is so much personal preference, so you decide which option suits you best.
Here's an example that should work:
Code:
#include <amxmodx>
#include <fun>
#define PLUGIN "MISC Killstreak"
#define VERSION "1.0"
#define AUTHOR "subs"
new g_MsgScoreInfo
new g_Kills[33]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("DeathMsg", "msgDeath", "a", "1>0")
set_task(1.0, "showKillstreak", 0, "", 0, "b")
g_MsgScoreInfo = get_user_msgid("ScoreInfo")
}
public msgDeath(id)
{
new killer = read_data(1)
new victim = read_data(2)
if ( is_user_connected(victim) )
g_Kills[victim] = 0
if ( is_user_connected(killer) )
{
g_Kills[killer]++ // Start killstreak counter
set_user_frags(killer, get_user_frags(killer) + clamp(g_Kills[killer] / 5, 0, 2))
message_begin(MSG_ALL, g_MsgScoreInfo) // Add points to scoreboard
write_byte(killer)
write_short(get_user_frags(killer))
write_short(get_user_deaths(killer))
write_short(0)
write_short(get_user_team(killer))
message_end()
}
}
public showKillstreak(id)
{
set_hudmessage(0, 95, 245, 0.0, 0.0, 0, 0.0, 1.1, 0.1, 0.5, 15)
show_hudmessage(id, "Killstreak: %i", g_Kills)
}
__________________