Bullet Collision Help - Bullet Despawns Upon Touching Gun

This is my current code:

local bullet = game.ReplicatedStorage.LaserProjectile:Clone()
bullet.Parent = workspace
bullet.CanCollide = false
bullet.Anchored = false
bullet.CFrame = gun.Handle.Muzzle.CFrame
	
bullet:SetNetworkOwner(player)
	
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Parent = bullet
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = bullet.CFrame.LookVector * 100
	
bullet.Touched:Connect(function(hit)
	print(hit)
	if hit.Parent ~= player.Character and hit.Parent ~= gun then
		print("boo")
		bullet:Destroy()
		if hit then
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage.Value)
			end
		end
	end
end)
	
task.wait(50)
bullet:Destroy()
  1. What do you want to achieve? I want my bullet to despawn upon hitting anything EXCEPT the player or the gun.

  2. What is the issue?
    Context: My bullet just moves forward because I am making a 2d top down shooter game, My player can dash forward and my character’s face conveniently has a part named “Face Decal” due to HDify plugin.
    Problem:
    Since Face Decal.Parent is the head and not the character, the bullet despawns. The bullet also despawns upon hitting the gun or the handle of the gun.
    Furthermore, if you notice it carefully the bullet only despawns after travelling past the touched part (can’t tell if this is due to server-client issues or not).

That’s not the full script is it? Cuz I can’t see where the gun variable was created. Anyways, if the gun is a model, then I don’t think the “hit.Parent ~= gun” part would get registered, it has to be the parts of the gun (eg, gun:GetChildren()).

Yes it isn’t the full script, I believe the gun variable is just game.StarterPack.LaserGun (the tool).

Basically, you are relying on the bullet to touch stuff for it to actually activate the Touched event. So, anything that isn’t physical will not be recognised. Models, Tools and such are not physical. Am only saying this because I’m assuming that you have a handle (the actual part) within the Tool, so, refer to the handle and not tool

Also for the Face Decal, you can get the Descendants of the Character instead of only children.

use :IsDescendantOf() instead

if Hit:IsDescendantOf(Character) then

else
-- bullet hit stuff here
end

and yes, it already registers for the gun collisions too since the gun is also a descendant of the character when equipped

3 Likes

You have to set the gun variable equal to the gun inside of the player’s Character, not StarterPack, because the gun in StarterPack and the gun held in the player’s Character aren’t the same.

my bad, i’m new to roblox lua and i forgot my script did the below code and not game.StarterPack.LaserGun

local gun = player.Character:FindFirstChild("LaserGun")

however, do you know how to find the gun inside the player’s character and not starterpack

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.