Bullet Script Issue

Gun Bullet Script

ISSUE
Currently if the bullet is shot, it can hit the player I am aiming at and it can also hit myself. I don’t want it to run the script if it hits myself. I only want the script to work if it hits the other players. I’m not sure what to write in the script to avoid the bullet script impacting me the shooter.

I am using hit such as below

script.Parent.Touched:Connect(function(hit)

     local player = hit.Parent
          local torso = player.HumanoidRootPart 

		if torso then
                
                   if player then



                    end
          end
end)



1 Like

Try the Search button first.
Are you using Touched for the bullet to do the damage? Raycasting might be better.
I found this post that seems to be pretty popular:
<Making a combat game with ranged weapons? FastCast may be the module for you!

I thought about WhiteList and BlackList b/c I just learned about that in my tutor sessions… but it was a bit different project I accomplished. I wasn’t sure if I could also use it to shoot a player without affecting me. I still wouldn’t know what to put in the script to tell it I the shooter should not be affected.

What differentiates me from the other players so that the script knows not to affect me ? I don’t know the answer to this.

I am using Touched(hit)

But im not causing damage, my bullet does something when it hits, it just does not cause harm to the players health.

You should use Region3 or Raycast for projectiles, another thing I recommend is doing :FindFirstChild("HumanoidRootPart") instead of .HumanoidRootPart.

so use this:
local torso = FindFirstChild:(“HumanoidRootPart”)

local HRP = player:FindFirstChild("HumanoidRootPart")

I tried that and It still impacts me if the bullet hits me while I’m the shooter.

Is the script that does that in the bullet?

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent
	
	numberofteleports = 8
	
	if player then

		
		local HRP = player:FindFirstChild("HumanoidRootPart")
		--local torso = player.HumanoidRootPart
		if HRP then
			for i = 1, numberofteleports do
				n = math.random(1,8)
				if n == 1 then 
					
					--torso.Position = Vector3.new(1147.297, -31.026, 155.285)
					HRP.CFrame = CFrame.new(-84, 150, -224)
						elseif n == 2 then
						
					HRP.CFrame = CFrame.new(98, 150, -224)
						elseif n == 3 then
			
					HRP.CFrame = CFrame.new(-84, 150, 224)
						elseif n == 4 then
					
					HRP.CFrame = CFrame.new(98, 150, 224)
						elseif n == 5 then
					
					HRP.CFrame = CFrame.new(-84, 100, -224)
						elseif n == 6 then
					
					HRP.CFrame = CFrame.new(98, 100, -224)
						elseif n == 7 then
					
					HRP.CFrame = CFrame.new(-84, 100, 224)
						elseif n == 8 then
					
					HRP.CFrame = CFrame.new(98,100,224)
					
				end
				
				
				
				
				
			end
			
		end
	end


end)

Yes the script is in the Bullet, the bullet is in Replicated Storage

In another game I was able to make the bullet only impact the monster and not other players by doing this
if hit.Parent:FindFirstChild(“monster”) then

But in order to enable this, In Explorer under the monster I had to change Character object which was named monster, I had to change the Humanoid to the name of monster.
I can’t do that with every shooter who picks up the gun and decides to shoot it in my game can I ?

Example of what I am talking about.

Humanoid screenshot Humanoid to monster example

Bullet impacts the shooter.wmv (4.6 MB)

Maybe I can use this:

if plr.Name ~= player.Name

Why not shoot the bullet from a couple of studs from the end of the gun instead of right at the location of the gun?

1 Like

Ok, I’m not certain how this will change it from happening but currently my bullet is as follows:

local cframe = Gun.Barrel.CFrame
bullet.CFrame = cframe

The CFrame of the barrel which is at the very tip of the gun. So you are saying make the CFrame a little before this position.

All-righty I tried your suggestion and all though it still happens occasionally it seems to of helped prevent myself from being hit most of the time. In the attached video I only get hit one time as opposed to the prior video I was getting hit a-lot.

bullet.CFrame = cframe * CFrame.new(0,0,-5)

Bullet with CFrame change.wmv (9.4 MB)

Just see if the hit part is apart of the shooter’s character and if it is then make the bullet keep going.