• ChatPrint error
    4 replies, posted
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. [IMG]http://images.akamai.steamusercontent.com/ugc/108481898363324551/FA96AB40411526A6E90FB346D06C3CC67104DCF7/[/IMG] Anyway, here is the code: [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 [/code] It's not much, but I still have no clue. I appreciate every help.
This is a thing due to SWEP prediction. Sometimes a SWEP calls the same function multiple times for [URL="http://wiki.garrysmod.com/page/Prediction"]reasons described here[/URL], so try using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsFirstTimePredicted]IsFirstTimePredicted[/url] in your functions [editline]20th December 2016[/editline] Just add [CODE] if not IsFirstTimePredicted() then return end [/CODE] At the top of each of the PrimaryAttack, SecondaryAttack, and Reload functions, hopefully it'll work
It's still not working. [code] AddCSLuaFile() SWEP.Base = "weapon_base" SWEP.PrintName = "TEST" SWEP.Spawnable = true SWEP.Author = "swagoverload.exe" SWEP.Instructions = "Right-click to select a target, left-click to make a swap and reload to deselect your target!" SWEP.DrawAmmo = false SWEP.DrawCrosshair = true SWEP.Slot = 0 SWEP.SlotPos = 5 SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" function SWEP:SecondaryAttack() local owner = self.Owner local target = self.Owner:GetEyeTrace().Entity if not IsFirstTimePredicted() then return end 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 not IsFirstTimePredicted() then return end 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 not IsFirstTimePredicted() then return end if !self:GetTargetEnt() then return "failed" else self:SetTargetEnt( nil ) self.Owner:ChatPrint( "Your target has been deselected" ) end end [/code] This is the whole code.
Maybe its because its getting called serverside and clientside? Try setting it only on the server making an if SERVER then check
Yes, it works. Thank you very much!
Sorry, you need to Log In to post a reply to this thread.