Hello everybody. I have a problem with the ChatPrint function.
I made a SWEP with which you can swap your position with another player.
You select a player with the right mouse button and you swap you positions with the left mouse button and you deselect the selected player with “R”.
This works fine.
You get a chat message for each action. ( “You’ve selected Bot01”, “Swapped position with Bot01.” etc. )
The weird thing is that it prints the messages twice and I don’t know why.
Anyway, here is the code:
function SWEP:SecondaryAttack() local owner = self.Owner local target = self.Owner:GetEyeTrace().Entity if target:IsPlayer() && target:Alive() then owner:ChatPrint( "You've selected " .. target:Nick() .. "" ) print( target ) return self:SetTargetEnt( target ) elseif ( target:IsPlayer() && !target:Alive() ) || ( !target:IsPlayer() ) then owner:ChatPrint( "No target was found!" ) end end function SWEP:SetTargetEnt( ent ) self.TargetEnt = ent end function SWEP:GetTargetEnt() return self.TargetEnt end function SWEP:PrimaryAttack() local owner = self.Owner local target = self.TargetEnt if IsValid( target ) then if ( target:Alive() ) then local selfpos = owner:GetPos() local entpos = self.TargetEnt:GetPos() owner:SetPos( entpos ) target:SetPos( selfpos ) owner:ChatPrint( "Swapped position with " .. target:Nick() .. "." ) self:Remove() else owner:ChatPrint( "The target is dead!" ) end else owner:ChatPrint( "No target is selected: Right-click on a player to select one." ) end end function SWEP:Reload() if !self:GetTargetEnt() then return "failed" else self:SetTargetEnt( nil ) self.Owner:ChatPrint( "Your target has been deselected" ) end end
It’s not much, but I still have no clue.
I appreciate every help.