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()
What do you want to achieve? I want my bullet to despawn upon hitting anything EXCEPT the player or the gun.
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()).
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
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.