• Avoiding Karma loss for a player affected by the Traitor Chip
    9 replies, posted
Hello! I've been working on fixing a TTT weapon called the Traitor Chip, but i've had no luck!. The weapon allows traitors to place a chip on the back of someone's head, then the traitor would left click the remote to make the chipped player misfire their gun. However the chipped player loses karma if injures or kills an innocent/Detective. Can anyone help fix this? CODE [code] SWEP.Base = "weapon_tttbase" SWEP.PrintName = "Trigger-Finger Chip" SWEP.Author = "The one-free man & Therma" SWEP.Slot = 7 SWEP.HoldType = "slam" SWEP.ViewModel = "models/freeman/v_mindcontroller.mdl" SWEP.WorldModel = "models/freeman/mindcontroller.mdl" SWEP.BobScale = 0 SWEP.SwayScale = 0 SWEP.ViewModelFlip = false SWEP.MaxPlaceDistance = 64 SWEP.Charges = 8 SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = { ROLE_TRAITOR } SWEP.EquipMenuData = { [ "type" ] = "Weapon", [ "name" ] = "Trigger-Finger Chip", [ "desc" ] = "Attach this device to the back of your victims head,\nrun off to a secure location and press the button to\nfire the victims weapon.\n\nWatch as the players blame the victim!" } SWEP.NoSights = true SWEP.Icon = "vgui/ttt/ttt_mindchip.vmt" function SWEP:SetupDataTables() self:NetworkVar( "Entity", 0, "Target" ) self:NetworkVar( "String", 0, "Error" ) self:NetworkVar( "Bool", 0, "Placed" ) self:NetworkVar( "Int", 0, "Shots" ) end function SWEP:Initialize() self:SetWeaponHoldType( self.HoldType ) if CLIENT then if not IsValid( self.viewModelChip ) then local pl = self:GetOwner() if IsValid( pl ) and pl == LocalPlayer() then local chip = ents.CreateClientProp() chip:SetModel( "models/freeman/mindchip.mdl" ) chip:SetPos( pl:GetPos() ) chip:SetParent( pl ) chip:SetNoDraw( true ) self.viewModelChip = chip end end end end SWEP.Automatic = { "weapon_zm_mac10", "weapon_ttt_m16", "weapon_zm_sledge" } if SERVER then AddCSLuaFile() function SWEP:IdleAnimate() local pl = self:GetOwner() local viewModel = pl:GetViewModel() local idleSequence = viewModel:LookupSequence( "idle01" ) viewModel:ResetSequence( idleSequence ) end function SWEP:PlaySequence( sequence ) local pl = self:GetOwner() local viewModel = pl:GetViewModel() self:IdleAnimate() timer.Simple( FrameTime(), function() if not IsValid( self ) then return end local sequence = viewModel:LookupSequence( sequence ) viewModel:ResetSequence( sequence ) if sequence != 3 then self.sequenceEnd = CurTime() + self:SequenceDuration() - 0.4 end end ) end function SWEP:CanPrimaryAttack() return self:GetShots() > 0 end function SWEP:PrimaryAttack( tbl ) local target = self:GetTarget() if not IsValid( target ) then local placeDist = ( TRAITORCHIP and TRAITORCHIP.MaxPlaceDistance ) or self.MaxPlaceDistance local pl = self:GetOwner() local tr = util.QuickTrace( pl:GetShootPos(), pl:EyeAngles():Forward() * placeDist, pl ) if tr.Hit then local pos = tr.HitPos if pos:Distance( pl:GetShootPos() ) <= placeDist then local ent = tr.Entity if IsValid( ent ) and ent:IsPlayer() and ent != pl then if not ent:HasChip() then local group = tr.HitGroup if group == HITGROUP_HEAD then local yawDelta = math.abs( math.AngleDifference( ent:GetAngles().y, pl:EyeAngles().y ) ) if yawDelta <= 64 then self:SetPlaced( true ) self:SetTarget( ent ) self:SetShots( ( TRAITORCHIP and TRAITORCHIP.Charges ) or self.Charges ) ent:SetChip( true ) self:PlaySequence( "draw" ) end end end end end end elseif self:GetPlaced() then if not self.nextAttack or self.nextAttack < CurTime() then self:PlaySequence( "press" ) timer.Simple( 0.2, function() -- Button press delay if not IsValid( self ) or not IsValid( self:GetOwner() ) then return end local sound = CreateSound( self:GetOwner(), "buttons/button18.wav" ) sound:PlayEx( 0.1, 196 ) local target = self:GetTarget() if IsValid( target ) then if target:Alive() then local wep = target:GetActiveWeapon() if IsValid( wep ) and wep:GetClass() != "weapon_ttt_unarmed" then local isController = wep:GetClass() == self:GetClass() local overflow = isController and tbl and table.HasValue( tbl, wep:GetTarget() ) if not overflow then if wep.CanPrimaryAttack and wep:CanPrimaryAttack() and wep.PrimaryAttack then if self:GetShots() > 0 then if isController then local tbl = tbl or { self:GetOwner() } wep:PrimaryAttack( tbl ) else local count = ( TRAITORCHIP.AutomaticWeapons and TRAITORCHIP.AutomaticWeapons[ wep:GetClass() ] ) or 1 for i = 0, count - 1 do timer.Simple( wep.Primary.Delay * i, function() if IsValid( wep ) and IsValid( target ) then if wep:CanPrimaryAttack() then target.isChipControlled = true target.controlledBy = self:GetOwner() wep:PrimaryAttack( true ) target.isChipControlled = false end end end ) end end self:SetShots( self:GetShots() - 1 ) self:GetTarget():SetAnimation( PLAYER_ATTACK1 ) self:SetError( "" ) else self:SetError( "No charges" ) end else self:SetError( "No ammo" ) end elseif tbl then for _, pl in pairs( tbl ) do if IsValid( pl ) then local wep = pl:GetActiveWeapon() if wep and wep:GetClass() == self:GetClass() then wep:SetError( "Overflow" ) end end end end else self:SetError( "No weapon" ) end else self:SetError( "Target dead" ) end end end ) self.nextAttack = CurTime() + 0.75 end end end function SWEP:Deploy() if not self:GetPlaced() then self:PlaySequence( "putaway" ) else self:PlaySequence( "draw" ) end end function SWEP:SecondaryAttack() end function SWEP:Think() if self.sequenceEnd then if self.sequenceEnd < CurTime() then self:IdleAnimate() self.sequenceEnd = nil end end end else surface.CreateFont( "TraitorChip", { [ "font" ] = "Trebuchet24", [ "size" ] = 32, [ "weight" ] = 0 } ) function SWEP:Think() if not self:GetPlaced() then local pl = self:GetOwner() if IsValid( pl ) and pl == LocalPlayer() then if not IsValid( self.viewModelChip ) then self:Initialize() end end end end function SWEP:ViewModelDrawn() local chip
There is a hook to prevent karma from being penalized: TTTKarmaGivePenalty(ply, penalty, victim) Perform a check in there and return true to prevent the penalty.
[QUOTE=man with hat;50753869]There is a hook to prevent karma from being penalized: TTTKarmaGivePenalty(ply, penalty, victim) Perform a check in there and return true to prevent the penalty.[/QUOTE] I'm honestly not that good at lua. :ohno: Could you show me how to achieve this? Or at least an example?
[CODE] if SERVER then hook.Add("TTTKarmaGivePenalty", "TTTTraitorChip", function(ply, penalty, victim) if ply:HasChip() then return true end end) end [/CODE] [editline]23rd July 2016[/editline] Put that in a shared .lua file or a serverside .lua file.
[QUOTE=geferon;50758535][CODE] if SERVER then hook.Add("TTTKarmaGivePenalty", "TTTTraitorChip", function(ply, penalty, victim) if ply:HasChip() then return true end end) end [/CODE] [editline]23rd July 2016[/editline] Put that in a shared .lua file or a serverside .lua file.[/QUOTE] Great that worked perfectly!! I have one last issue. Is there a way to make an inno/detective who kills a person that has been chipped, in self defense, not lose karma?
[QUOTE=ncc360;50759189]Great that worked perfectly!! I have one last issue. Is there a way to make an inno/detective who kills a person that has been chipped, in self defense, not lose karma?[/QUOTE] same thing but checking if the victim is the one that HasChip.
[QUOTE=geferon;50759941]same thing but checking if the victim is the one that HasChip.[/QUOTE] Like this? [code] SERVER then hook.Add("TTTKarmaGivePenalty", "TTTTraitorChip", function(ply, penalty, target) if ply:HasChip() then return true end end) end [/code]
i think if target:HasChip()
[QUOTE=ncc360;50759975]Like this? [code] SERVER then hook.Add("TTTKarmaGivePenalty", "TTTTraitorChip", function(ply, penalty, target) if ply:HasChip() then return true end end) end [/code][/QUOTE] When i said victim i was meaning the 3rd variable i assigned to the hook function. In your case it would be target. Also, the target can as well be a normal entity or an npc so check as well before if it is a player.
This should work as what you are using it for. [CODE] if SERVER then local function KarmaHurt(ply, victim, penalty) if not IsValid(ply) or not IsValid(victim) then return end if ply == victim then return end if not ply:IsPlayer() or not victim:IsPlayer() then return end if ply:GetTraitor() == victim:GetTraitor() then KARMA.GivePenalty(ply, penalty, victim) ply:SetCleanRound(false) end end hook.Add("TTTKarmaGivePenalty", "TTTTraitorChip", function(ply, penalty, victim) if ply:HasChip() and (ply:HasChip() and ply.isChipControlled) then KarmaHurt(ply.controlledBy, victim, penalty) return true end end) end [/CODE]
Sorry, you need to Log In to post a reply to this thread.