Using a Metatable to store a variable for a specific person

Suppose I have a function like this:

[lua]local plymeta = FindMetaTable( “Player” )

function plymeta:killStreak(action)
if action == “kill” then

elseif action == "death" then
	
end

end[/lua]

To keep track of a killstreak for a specific person.

How would I store a variable inside this thing?

I have another function passing arguments to this one, which isn’t important.

You could do self.Killstreak = x

Thanks, let me see if it works. I don’t have what I need to test it yet, though. I will update this thread once I know it works.

[lua]self.Killstreaks = self.Killstreaks + 1[/lua]

As long as this is server side, it can be accessed anywhere server side if you index the player with the same variable.

Gotta make sure it isn’t nil though.
[lua]self.Killstreaks = self.Killstreaks and self.Killstreaks + 1 or 1[/lua]

This doesn’t seem to work, and I don’t know why.
[lua]/* Find the player metatable and store it into a variable for easier use */

local plymeta = FindMetaTable( “Player” )

/* first blood */

if SERVER then
serverkills = 0;
function GM:PlayerDeath( victim, inflictor, attacker )
/* check to see if the attacker killed himself, if so continue with recording first blood */
if victim != attacker then
if serverkills = 0 then
sound.Play( “multikills/firstblood.mp3”, Vector(0, 0, 0) )
end
serverkills = serverkills + 1
end
end
end

/* the master function to keep track of killstreaks */

function plymeta:killStreak(action)
self.killStreak = ks
ks = 0
if action == “kill” then
ks = ks + 1
/In the same function for convenince, checks to see if they player is on a killstreak/
if ks == 3 then
killStreakAnnouncer(3)
end
/End of killstreak checking/
elseif action == “death” then
ks = 0
end
end

/* the hook to pass kills and deaths to the master function */

function GM:PlayerDeath( victim, inflictor, attacker )
if victim == attacker then
attacker:killStreak(“death”)
else then
attacker:killStreak(“kill”)
end
end

/Guy who says KILLING SPREE/

function killStreakAnnouncer(number)
if number == 3 then
sound.Play( “killingspree.mp3”, Vector(0, 0, 0) )
end

end[/lua]

I can confirm the mp3 file works in gmod

No errors, just nothing happens.

Your code was just resetting ks every time it was run…



function plymeta:killStreak(action)
        self.killStreak = self.killStreak or 0
	if action == "kill" then
		self.killStreak = self.killStreak + 1
		/*In the same function for convenince, checks to see if they player is on a killstreak*/
		if self.killStreak == 3 then
			killStreakAnnouncer(3)
		end
		/*End of killstreak checking*/
	elseif action == "death" then
		self.killStreak = 0
	end
end


Same results…

-snip- i see the problem, 1 second.

[editline]8th March 2015[/editline]



local plymeta = FindMetaTable( "Player" )

if SERVER then
	firstblood = false;
	function GM:PlayerDeath( victim, inflictor, attacker )
		victim.kills = 0
		if victim != attacker then
			if !firstblood then
				sound.Play( "multikills/firstblood.mp3", Vector(0, 0, 0) )
				firstblood = true
			end
			attacker.kills = attacker.kills + 1 or 1
			if attacker.kills == 3 then
				attacker:SendLua("sound.Play( 'killingspree.mp3', Vector(0, 0, 0) )")
			end
		end
	end
end


You had server only and client only stuff in a shared file, which is confusing.
Also, sorry about indentation, blame Garry.

Here’s one I wrote a while back: https://dl.dropboxusercontent.com/u/26074909/tutoring/_gamemode_logic/gun_race/sv_upgrade_weapons_for_kills.lua.html

and

https://dl.dropboxusercontent.com/u/26074909/tutoring/_gamemode_logic/gun_race/sv_gun_race.lua.html

You would need to use hook.Add instead of GM: unless you’re writing a game-mode. The gun-race logic above tracks kills using either Frags, or a different variable.

Tried to get this to work.

Couldn’t. No matter what nothing works.

I give up.