how do i make a lua script that triggers when a player has "x" amount of kills? :quagmire:
I only know the basics of (Java) coding, but I'd imagine you'd need to run some sort of counter every time a person gets a kill and when that counter reaches a certain number, it activates the LUA script.
[QUOTE=Alsojames;42685643]I only know the basics of (Java) coding, but I'd imagine you'd need to run some sort of counter every time a person gets a kill and when that counter reaches a certain number, it activates the LUA script.[/QUOTE]
pretty much this
[lua]
hook.Add("PlayerDeath","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%10==0 then
attacker:ChatPrint("WICKED SICK!!")
end
end)
[/lua]
Every 10 kills tells the player how awesome they are.
[QUOTE=hootiegerbil;42692844][lua]
hook.Add("PlayerDeath","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%10==0 then
attacker:ChatPrint("WICKED SICK!!")
end
end)
[/lua]
Every 10 kills tells the player how awesome they are.[/QUOTE]
do i just change the ChatPrint into something else to make something else happen?
[editline]30th October 2013[/editline]
so does this give me a weapon at 5 kills?
hook.Add("PlayerDeath","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%5==0 then
attacker:Player:Give("weapon_stunstick")
end
end)
[editline]30th October 2013[/editline]
`gives an error
[ERROR] lua/autorun/server/script gmod.lua:5: 'then' expected near 'end'
1. unknown - lua/autorun/server/script gmod.lua:0
You don't need the space between the p and the ly. Also, since you're trying to give the weapon to the attacker you would do attacker, you don't do attacker:Player. If you were trying to give it to the player that died then you would do ply:Give instead of "Player" since "Player" isn't defined anywhere. Anyways here's the fixed code.
[lua]
hook.Add("PlayerDeath","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%5==0 then
attacker:Give("weapon_stunstick")
end
end)
[/lua]
[ERROR] lua/autorun/server/payoff.lua:8: attempt to call method 'Frags' (a nil value)
1. v - lua/autorun/server/payoff.lua:8
2. unknown - lua/includes/modules/hook.lua:82
[editline]31st October 2013[/editline]
[QUOTE]hook.Add("PlayerDeath","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%5==0 then
attacker:Give("weapon_stunstick")
end
end)
hook.Add("OnNPCKilled","CheckKillCount",function(ply,dmg,attacker)
if attacker:Frags()%5==0 then
attacker:Give("weapon_stunstick")
end
end)[/QUOTE]
[editline]31st October 2013[/editline]
`the space between p and ly is automatic when copied...
[editline]31st October 2013[/editline]
YES i made the lua script :))))) thanks to all of you!!!!!!!!!!!!!!!!!!!!
last question, how do i reset scores on death because you can only earn the weapon once...
[QUOTE=mpolder;42711657][ERROR] lua/autorun/server/payoff.lua:8: attempt to call method 'Frags' (a nil value)
1. v - lua/autorun/server/payoff.lua:8
2. unknown - lua/includes/modules/hook.lua:82
[editline]31st October 2013[/editline]
[editline]31st October 2013[/editline]
`the space between p and ly is automatic when copied...
[editline]31st October 2013[/editline]
YES i made the lua script :))))) thanks to all of you!!!!!!!!!!!!!!!!!!!!
last question, how do i reset scores on death because you can only earn the weapon once...[/QUOTE]
Each time someone gets a kill, store that into an array, each time one dies, zero the score of the dead.
That should allow you to keep track. (I guess you're trying something like gun game?)
I am but it's my second lua script ever so i'm having some trouble, it gives the weapon on 1 kill but i want that to be 5
How do i reset the killcounter?
[CODE]
ply:SetFrags(0)
[/CODE]
Change ply to whatever you are referring to the dead as...
Have you tried looking here? [URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4875.html[/URL]
It says its outdated and some of it may be, but there is still some great content there (PS. You may find yourself falling off the site now and again due to dead links)
[QUOTE=spikeman14;42721796][CODE]
ply:SetFrags(0)
[/CODE]
Change ply to whatever you are referring to the dead as...
Have you tried looking here? [URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4875.html[/URL]
It says its outdated and some of it may be, but there is still some great content there (PS. You may find yourself falling off the site now and again due to dead links)[/QUOTE]
im currently trying this, do you think this will work?
[QUOTE]function PlayerDeath( num )
Player:SetFrags(0)
end
end[/QUOTE]
No, because that's not valid code:
PlayerDeath passes more than one argument.
"Player" isn't a player object.
You only need one end.
The function isn't being called, so it is never ran.
Untested, but something like this might work:
[lua]
local rewards = { --key = streak, value = reward
[5] = "weapon_stunstick",
}
local function OnPlayerDeath( ply, inflictor, attacker )
if ply ~= attacker then --Dont do any of the below if the player kills themself.
ply.streak = 0 --Set the victim's streak to 0.
attacker.streak = attacker.streak or 0 --Define the attacker's streak, if it's an existing variable, set it to that, otherwise set to 0.
attacker.streak = attacker.streak + 1 --Add one to the streak.
if rewards[attacker.streak] then --Check if the streak is high enough for a reward.
attacker:Give( rewards[attacker.streak] ) --Give the attacker the weapon.
end
end
end
hook.Add( "PlayerDeath", "OnPlayerDeath", OnPlayerDeath )
[/lua]
[QUOTE=Greetings;42722245]No, because that's not valid code:
PlayerDeath passes more than one argument.
"Player" isn't a player object.
You only need one end.
The function isn't being called, so it is never ran.
Untested, but something like this might work:
[lua]
local rewards = { --key = streak, value = reward
[5] = "weapon_stunstick",
}
local function OnPlayerDeath( ply, inflictor, attacker )
if ply ~= attacker then --Dont do any of the below if the player kills themself.
ply.streak = 0 --Set the victim's streak to 0.
attacker.streak = attacker.streak or 0 --Define the attacker's streak, if it's an existing variable, set it to that, otherwise set to 0.
attacker.streak = attacker.streak + 1 --Add one to the streak.
if rewards[attacker.streak] then --Check if the streak is high enough for a reward.
attacker:Give( rewards[attacker.streak] ) --Give the attacker the weapon.
end
end
end
hook.Add( "PlayerDeath", "OnPlayerDeath", OnPlayerDeath )
[/lua][/QUOTE]
does not seem to work, i dont get any weapons at the 5 kills....
[QUOTE=mpolder;42722961]does not seem to work, i dont get any weapons at the 5 kills....[/QUOTE]
Any errors in console? I can't see anything wrong with it by just glancing over it, but my mind is currently in PHP mode!
[QUOTE=mpolder;42722961]does not seem to work, i dont get any weapons at the 5 kills....[/QUOTE]
Just tested it, and it works fine for me. Are you running it server-side?
[QUOTE=Greetings;42723194]Just tested it, and it works fine for me. Are you running it server-side?[/QUOTE]
ok ill look again in a sec but it didnt work last time.. or does it only work on players?
and my plan is to put it on a server
[QUOTE=mpolder;42723368]ok ill look again in a sec but it didnt work last time.. or does it only work on players?
and my plan is to put it on a server[/QUOTE]
Works only on players.
Adding NPCs is just more of the same.
[lua]
if not SERVER then
error( "This is here to tell you that you are dumb." )
end
local rewards = { --key = streak, value = reward
[5] = "weapon_stunstick",
}
local function OnPlayerDeath( ply, inflictor, attacker )
if ply ~= attacker then --Dont do any of the below if the player kills themself.
ply.streak = 0 --Set the victim's streak to 0.
attacker.streak = attacker.streak or 0 --Define the attacker's streak, if it's an existing variable, set it to that, otherwise set to 0.
attacker.streak = attacker.streak + 1 --Add one to the streak.
if rewards[attacker.streak] then --Check if the streak is high enough for a reward.
attacker:Give( rewards[attacker.streak] ) --Give the attacker the weapon.
end
end
end
hook.Add( "PlayerDeath", "OnPlayerDeath", OnPlayerDeath )
local function OnNPCDeath( npc, killer, weapon )
if killer:IsPlayer() then
killer.streak = killer.streak or 0
killer.streak = killer.streak + 1
if rewards[killer.streak] then
killer:Give( rewards[killer.streak] )
end
end
end
hook.Add( "OnNPCKilled", "OnNPCDeath", OnNPCDeath )
[/lua]
[QUOTE=Greetings;42723434]Works only on players.
Adding NPCs is just more of the same.
[lua]
if not SERVER then
error( "This is here to tell you that you are dumb." )
end
local rewards = { --key = streak, value = reward
[5] = "weapon_stunstick",
}
local function OnPlayerDeath( ply, inflictor, attacker )
if ply ~= attacker then --Dont do any of the below if the player kills themself.
ply.streak = 0 --Set the victim's streak to 0.
attacker.streak = attacker.streak or 0 --Define the attacker's streak, if it's an existing variable, set it to that, otherwise set to 0.
attacker.streak = attacker.streak + 1 --Add one to the streak.
if rewards[attacker.streak] then --Check if the streak is high enough for a reward.
attacker:Give( rewards[attacker.streak] ) --Give the attacker the weapon.
end
end
end
hook.Add( "PlayerDeath", "OnPlayerDeath", OnPlayerDeath )
local function OnNPCDeath( npc, killer, weapon )
if killer:IsPlayer() then
killer.streak = killer.streak or 0
killer.streak = killer.streak + 1
if rewards[killer.streak] then
killer:Give( rewards[killer.streak] )
end
end
end
hook.Add( "OnNPCKilled", "OnNPCDeath", OnNPCDeath )
[/lua][/QUOTE]
going to test it right now :P
[editline]1st November 2013[/editline]
works very well thanks :P and btw i saw your message and it didnt get through
[editline]1st November 2013[/editline]
[QUOTE=Greetings;42723434]Works only on players.
Adding NPCs is just more of the same.
[lua]
if not SERVER then
error( "This is here to tell you that you are dumb." )
end
local rewards = { --key = streak, value = reward
[5] = "weapon_stunstick",
}
local function OnPlayerDeath( ply, inflictor, attacker )
if ply ~= attacker then --Dont do any of the below if the player kills themself.
ply.streak = 0 --Set the victim's streak to 0.
attacker.streak = attacker.streak or 0 --Define the attacker's streak, if it's an existing variable, set it to that, otherwise set to 0.
attacker.streak = attacker.streak + 1 --Add one to the streak.
if rewards[attacker.streak] then --Check if the streak is high enough for a reward.
attacker:Give( rewards[attacker.streak] ) --Give the attacker the weapon.
end
end
end
hook.Add( "PlayerDeath", "OnPlayerDeath", OnPlayerDeath )
local function OnNPCDeath( npc, killer, weapon )
if killer:IsPlayer() then
killer.streak = killer.streak or 0
killer.streak = killer.streak + 1
if rewards[killer.streak] then
killer:Give( rewards[killer.streak] )
end
end
end
hook.Add( "OnNPCKilled", "OnNPCDeath", OnNPCDeath )
[/lua][/QUOTE]
im getting this error... [ERROR] lua/autorun/asetfragsondeath.lua:19: '<eof>' expected near 'end'
1. unknown - lua/autorun/asetfragsondeath.lua:0
[QUOTE=mpolder;42723738]going to test it right now :P
[editline]1st November 2013[/editline]
works very well thanks :P and btw i saw your message and it didnt get through
[editline]1st November 2013[/editline]
im getting this error... [ERROR] lua/autorun/asetfragsondeath.lua:19: '<eof>' expected near 'end'
1. unknown - lua/autorun/asetfragsondeath.lua:0[/QUOTE]
There are no errors for me, your end's look ok
Sorry, you need to Log In to post a reply to this thread.