TTT knife question

Is there a way to make it so that you can only knife someone if 4 players are alive? (1 Traitor, 3 Innocents - or 2 Traitor…)

Yes.

One way would be to prevent the weapon from being equipped until that many players are alive. Additionally, I’d prevent it from being PURCHASED unless 4 players are alive to prevent people feeling as though they wasted a credit.

Modify / Hook into:
GM:PlayerSwitchWeapon( player, currentWeapon, newWeapon )

return true; to prevent the weapon being equipped.

Basically:

[lua]//
//
//
local function PlayersAlive( )
local _count = 0;
for k, _p in pairs( player.GetAll( ) ) do
if ( _p:Alive( ) ) then _count = _count + 1; end
end
return _count;
end

//
//
//
hook.Add( “PlayerSwitchWeapon”, “NoKnifey”, function( _p, _w, _wNew )
if ( _wNew:GetClass( ) == “ttt_knife” ) then
if ( PlayersAlive( ) > 4 ) then
// Prevent them from pulling knife
return true;
end
end
end );[/lua]

Nice, thank you for reply. :slight_smile:

No problem, if that solved your question please mark this thread solved in top left corner.

Ich bin ja auch Deutscher!

Oh, Make sure you change code to limit buying the knife until the same boolean statement is true… You could also prevent damage from happening ( but that’s an easy way to get a traitor killed, so I wouldn’t recommend it, stop it before it happens otherwise people will be pissed at your server for allowing that to happen )

I did not use your code, only a snip of it. (The part where checked how many players are alive)
But thank you always.

You are german too right? Currently I am learning Lua an need help, could you accept my friend request? :slight_smile:

Players in spectator mode count as alive players, so use IsTerror() along with Alive()
[lua]//
//
//
local function PlayersAlive( )
local count = 0
for k, p in pairs( player.GetAll( ) ) do
if ( p:Alive( ) and p:IsTerror() ) then count = count + 1 end
end
return count
end

//
//
//
hook.Add( “PlayerSwitchWeapon”, “NoKnifey”, function( _p, _w, _wNew )
if ( _wNew:GetClass( ) == “ttt_knife” ) then
if ( PlayersAlive( ) > 4 ) then
// Prevent them from pulling knife
return true;
end
end
end );[/lua]

You accepted :smiley:
Thank you, solved.

-edit-

Ok.