Need help with a new Traitor weapon for TTT

Hello,

the last few days a friend and I were trying to create a traitor item for ttt.
The item is a gun and with its shot the target gets a random ‘new role’,
either detecitve, traitor or innocent. Now we’re experiencing some minor bugs.
Sometimes the target changes its role, but can’t see any change in the bottom left nor the scoreboard.
He is only visible as the new role for the one who shot him, but still counts as its previous role. Also in some cases when the target gets shot,
the shown role changes between the possible ones(T/D) for half a second or less until it settles down on the final role.

Here’s the part of the code where our mistakes were probably made.
Thanks in advance for any help we’ll receive.


function SWEP:PrimaryAttack()
            
    local tr = self.Owner:GetEyeTrace();
    local tracedata = {};
    self:TakePrimaryAmmo(1);
    self:EmitSound( self.Primary.Sound, self.Primary.SoundLevel );
    tracedata.pos = tr.HitPos + Vector(0,0,2);
  	ply = tr.Entity;
    math.randomseed(os.time());
    h = math.random( 1, 9 );
    --local message = "Es wurde " .. h .. " gerollt.";
    --ply:PrintMessage(HUD_PRINTTALK, message );
            
        if h > 6 then
   	
        ply:SetRole(ROLE_TRAITOR);
   	SendFullStateUpdate();
    	ply:AddCredits ( 1 );
        
        elseif h > 3 then

  	ply:SetRole(ROLE_DETECTIVE);
    	SendFullStateUpdate();
    	ply:AddCredits ( 1 );
      	else
        
        ply:SetRole(ROLE_INNOCENT);
   	ply:SetTeam(TEAM_TERROR);
   	SendFullStateUpdate();
        end
            
      	if self.Owner:GetAmmoCount(self.Primary.Ammo) < 1 then
            self.Owner:DropWeapon( self );
            self:Remove();
            return false
        end
end


function SWEP:PrimaryAttack() 
	if self:Ammo1() < 1 then
		self:Remove()
		return
	end

	local tr = self.Owner:GetEyeTrace();
	local tracedata = {};
	self:EmitSound( self.Primary.Sound, self.Primary.SoundLevel );
	tracedata.pos = tr.HitPos + Vector(0,0,2);
	local target = tr.Entity;
	if !IsValid(target) or !target:IsPlayer() then return end
	--math.randomseed(os.time()); -- I do not know what is this
	local h = math.random( 1, 9 );
	--local message = "Es wurde " .. h .. " gerollt.";
	--target:PrintMessage(HUD_PRINTTALK, message );
            
	if h > 6 then
		target:SetRole(tonumber(ROLE_TRAITOR))
		target:SetCredits(1)
		SendFullStateUpdate()
	elseif h > 3 then
		target:SetRole(tonumber(ROLE_DETECTIVE))
		target:SetCredits(4)
		SendFullStateUpdate()
	else
		target:SetRole(tonumber(ROLE_INNOCENT))
		target:SetCredits(0)
		SendFullStateUpdate()
	end
	
	self:TakePrimaryAmmo(1);
end

Tanks mate,

i had not much time to test it but your code seems working except the remove part it looks like the weapon gets remove before the command can be executed. But i cant see any mistakes in your code.