Need Help Fixing Gun

Hi! Recently, I tried to make my own gun. I’m basically finished. Problem: The gun is somehow hitting myself, which is what I definitely wasn’t expecting.

Have you experience this before? If so, I’d be happy for you to place suggestions.

Could you send your script that does the damage for the bullets?

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	print(equipped)
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	print(equipped)
end)

local fired = false
local fireTime = 0
local eventTime = 0
local fireRate = 1

local function countCheck()
	if fireTime-eventTime >= fireRate or fireRate-(fireTime-eventTime) <= 1/30 then
		return true
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(function(player, mousePos)
	if not player.Character == script.Parent.Parent then return end
end)

local function newBullet()
	local bullet = Instance.new("Part")
	bullet.Name = "Bullet"
	bullet.Parent = workspace.BulletStorage
	bullet.Size = Vector3.new(0.3, 0.125, 0.125)
	bullet.Transparency = 0
	bullet.Position = script.Parent.Rifle.ShootingPoint.Position
	bullet.Color = Color3.new(1, 1, 0)
	bullet.Material = Enum.Material.Neon
	bullet.CanCollide = false
	bullet:SetNetworkOwner(nil)
	return bullet
end

local function applyPhysics(bullet, mousePos)
	local muzzleVelocity = 300
	local direction = CFrame.new(bullet.Position, mousePos)
	
	local horizontalSpread = 4
	local verticalSpread = 4
	
	horizontalSpread = math.rad(horizontalSpread)
	verticalSpread = math.rad(verticalSpread)
	
	direction = direction*CFrame.Angles(horizontalSpread, verticalSpread, 0)
	direction = direction.LookVector
	
	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction*mass*muzzleVelocity)
end

local function fireAmmo(player, mousePos)
	
	local bullet = newBullet()
	applyPhysics(bullet, mousePos)
	
	bullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)
		end
	end)

	local RNS = game:GetService("RunService")
	local lastPos = bullet.Position
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	local range = 300
	
	local tracer = workspace.Visual:GetChildren()[1]
	
	if tracer then
		tracer.Parent = workspace.Visual
	else
		tracer = Instance.new("Part")
		tracer.Anchored = true
		tracer.CanCollide = false
		tracer.Position = Vector3.new(0, 1e7, 0)
	end
	
	tracer.BrickColor = BrickColor.new("Gold")
	tracer.Material = Enum.Material.Neon
	
	while range > 0 do
		RNS.Heartbeat:Wait()
		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		lastPos = bullet.Position

		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos, rayPos).LookVector
		
		tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).Magnitude)
		tracer.CFrame = CFrame.new(rayPos*lastPos)*CFrame.new(0, 0, (-lastPos-rayPos).Magnitude/2)
		local ray = workspace:Raycast(lastPos, rayDirection*rayLength, rayFilter)
		if ray then
			bullet:Destroy()
			local hit = ray.Instance
			print(hit:GetFullName())
			local model = hit:FindFirstAncestorOfClass("Model")
			if model then
				local humanoid = model:FindFirstChildWhichIsA("Humanoid") or nil
				if humanoid then
					if hit.Head then
						humanoid:TakeDamage(45)
					else
						humanoid:TakeDamage(15)
					end
					return true
				end
			end
			break
		end
		
		if bullet.Parent == nil then
			bullet:Destroy()
			break
		end
	end

	if bullet then bullet:Destroy() end
end

script.Parent.MouseClicked.OnServerEvent:Connect(fireAmmo)

Try this:

local Players = game:GetService("Players")

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	print(equipped)
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	print(equipped)
end)

local fired = false
local fireTime = 0
local eventTime = 0
local fireRate = 1

local function countCheck()
	if fireTime-eventTime >= fireRate or fireRate-(fireTime-eventTime) <= 1/30 then
		return true
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(function(player, mousePos)
	if not player.Character == script.Parent.Parent then 
		return 
	end
end)

local function newBullet()
	local bullet = Instance.new("Part")
	bullet.Name = "Bullet"
	bullet.Parent = workspace.BulletStorage
	bullet.Size = Vector3.new(0.3, 0.125, 0.125)
	bullet.Transparency = 0
	bullet.Position = script.Parent.Rifle.ShootingPoint.Position
	bullet.Color = Color3.new(1, 1, 0)
	bullet.Material = Enum.Material.Neon
	bullet.CanCollide = false
	bullet:SetNetworkOwner(nil)
	return bullet
end

local function applyPhysics(bullet, mousePos)
	local muzzleVelocity = 300
	local direction = CFrame.new(bullet.Position, mousePos)

	local horizontalSpread = 4
	local verticalSpread = 4

	horizontalSpread = math.rad(horizontalSpread)
	verticalSpread = math.rad(verticalSpread)

	direction = direction*CFrame.Angles(horizontalSpread, verticalSpread, 0)
	direction = direction.LookVector

	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction*mass*muzzleVelocity)
end

local function fireAmmo(player, mousePos)
	local bullet = newBullet()
	applyPhysics(bullet, mousePos)

	bullet.Touched:Connect(function(hit)
		local Player = Players:GetPlayerFromCharacter(hit.Parent)
		
		if Player and Player ~= player then
			hit.Parent.Humanoid:TakeDamage(15)
		end
	end)

	local RNS = game:GetService("RunService")
	local lastPos = bullet.Position
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	local range = 300

	local tracer = workspace.Visual:GetChildren()[1]

	if tracer then
		tracer.Parent = workspace.Visual
	else
		tracer = Instance.new("Part")
		tracer.Anchored = true
		tracer.CanCollide = false
		tracer.Position = Vector3.new(0, 1e7, 0)
	end

	tracer.BrickColor = BrickColor.new("Gold")
	tracer.Material = Enum.Material.Neon

	while range > 0 do
		RNS.Heartbeat:Wait()
		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		lastPos = bullet.Position

		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos, rayPos).LookVector
		tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).Magnitude)
		tracer.CFrame = CFrame.new(rayPos*lastPos)*CFrame.new(0, 0, (-lastPos-rayPos).Magnitude/2)
		
		local ray = workspace:Raycast(lastPos, rayDirection*rayLength, rayFilter)
		
		if ray then
			bullet:Destroy()
			local hit = ray.Instance
			local hitPlayer = Players:GetPlayerFromCharacter(hit.Parent)
			
			if hitPlayer and hitPlayer ~= player then
				local humanoid = hitPlayer.Character:FindFirstChildWhichIsA("Humanoid") or nil
				
				if humanoid then
					if hit.Head then
						humanoid:TakeDamage(45)
					else
						humanoid:TakeDamage(15)
					end
					
					return true
				end
			end
			
			break
		end

		if not bullet.Parent then
			bullet:Destroy()
			
			break
		end
	end

	if bullet then 
		bullet:Destroy() 
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(fireAmmo)

It worked for me but it’s not dealing damage anymore.

Try this

local player = script.Parent.Parent.Parent
local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	print(equipped)
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	print(equipped)
end)

local fired = false
local fireTime = 0
local eventTime = 0
local fireRate = 1

local function countCheck()
	if fireTime-eventTime >= fireRate or fireRate-(fireTime-eventTime) <= 1/30 then
		return true
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(function(player, mousePos)
	if not player.Character == script.Parent.Parent then return end
end)

local function newBullet()
	local bullet = Instance.new("Part")
	bullet.Name = "Bullet"
	bullet.Parent = workspace.BulletStorage
	bullet.Size = Vector3.new(0.3, 0.125, 0.125)
	bullet.Transparency = 0
	bullet.Position = script.Parent.Rifle.ShootingPoint.Position
	bullet.Color = Color3.new(1, 1, 0)
	bullet.Material = Enum.Material.Neon
	bullet.CanCollide = false
	bullet:SetNetworkOwner(nil)
	return bullet
end

local function applyPhysics(bullet, mousePos)
	local muzzleVelocity = 300
	local direction = CFrame.new(bullet.Position, mousePos)

	local horizontalSpread = 4
	local verticalSpread = 4

	horizontalSpread = math.rad(horizontalSpread)
	verticalSpread = math.rad(verticalSpread)

	direction = direction*CFrame.Angles(horizontalSpread, verticalSpread, 0)
	direction = direction.LookVector

	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction*mass*muzzleVelocity)
end

local function fireAmmo(player, mousePos)

	local bullet = newBullet()
	applyPhysics(bullet, mousePos)

	bullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and not player.Character then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(15)
		end
	end)

	local RNS = game:GetService("RunService")
	local lastPos = bullet.Position
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	local range = 300

	local tracer = workspace.Visual:GetChildren()[1]

	if tracer then
		tracer.Parent = workspace.Visual
	else
		tracer = Instance.new("Part")
		tracer.Anchored = true
		tracer.CanCollide = false
		tracer.Position = Vector3.new(0, 1e7, 0)
	end

	tracer.BrickColor = BrickColor.new("Gold")
	tracer.Material = Enum.Material.Neon

	while range > 0 do
		RNS.Heartbeat:Wait()
		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		lastPos = bullet.Position

		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos, rayPos).LookVector

		tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).Magnitude)
		tracer.CFrame = CFrame.new(rayPos*lastPos)*CFrame.new(0, 0, (-lastPos-rayPos).Magnitude/2)
		local ray = workspace:Raycast(lastPos, rayDirection*rayLength, rayFilter)
		if ray then
			bullet:Destroy()
			local hit = ray.Instance
			print(hit:GetFullName())
			local model = hit:FindFirstAncestorOfClass("Model")
			if model then
				local humanoid = model:FindFirstChildWhichIsA("Humanoid") or nil
				if humanoid then
					if hit.Head then
						humanoid:TakeDamage(45)
					else
						humanoid:TakeDamage(15)
					end
					return true
				end
			end
			break
		end

		if bullet.Parent == nil then
			bullet:Destroy()
			break
		end
	end

	if bullet then bullet:Destroy() end
end

script.Parent.MouseClicked.OnServerEvent:Connect(fireAmmo)

Try this:


local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	print(equipped)
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	print(equipped)
end)

local fired = false
local fireTime = 0
local eventTime = 0
local fireRate = 1

local function countCheck()
	if fireTime-eventTime >= fireRate or fireRate-(fireTime-eventTime) <= 1/30 then
		return true
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(function(player, mousePos)
	if not player.Character == script.Parent.Parent then return end
end)

local function newBullet()
	local bullet = Instance.new("Part")
	bullet.Name = "Bullet"
	bullet.Parent = workspace.BulletStorage
	bullet.Size = Vector3.new(0.3, 0.125, 0.125)
	bullet.Transparency = 0
	bullet.Position = script.Parent.Rifle.ShootingPoint.Position
	bullet.Color = Color3.new(1, 1, 0)
	bullet.Material = Enum.Material.Neon
	bullet.CanCollide = false
	bullet:SetNetworkOwner(nil)
	
	return bullet
end

local function applyPhysics(bullet, mousePos)
	local muzzleVelocity = 300
	local direction = CFrame.new(bullet.Position, mousePos)

	local horizontalSpread = 4
	local verticalSpread = 4

	horizontalSpread = math.rad(horizontalSpread)
	verticalSpread = math.rad(verticalSpread)

	direction = direction*CFrame.Angles(horizontalSpread, verticalSpread, 0)
	direction = direction.LookVector

	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction*mass*muzzleVelocity)
end

local function fireAmmo(player, mousePos)

	local bullet = newBullet()
	
	task.delay(5, function()
		bullet:Destroy()
	end)
	
	applyPhysics(bullet, mousePos)

	bullet.Touched:Connect(function(hit)
		local character = hit.Parent
		
		if character and character ~= player.Character then
			local Humanoid = character:FindFirstChildWhichIsA("Humanoid")
			
			if Humanoid then
				Humanoid:TakeDamage(15)
			end
		end
	end)

	local RNS = game:GetService("RunService")
	local lastPos = bullet.Position
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	local range = 300

	local tracer = workspace.Visual:GetChildren()[1]

	if tracer then
		tracer.Parent = workspace.Visual
	else
		tracer = Instance.new("Part")
		tracer.Anchored = true
		tracer.CanCollide = false
		tracer.Position = Vector3.new(0, 1e7, 0)
	end

	tracer.BrickColor = BrickColor.new("Gold")
	tracer.Material = Enum.Material.Neon

	while range > 0 do
		RNS.Heartbeat:Wait()
		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		lastPos = bullet.Position

		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos, rayPos).LookVector

		tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).Magnitude)
		tracer.CFrame = CFrame.new(rayPos*lastPos)*CFrame.new(0, 0, (-lastPos-rayPos).Magnitude/2)
		local ray = workspace:Raycast(lastPos, rayDirection*rayLength, rayFilter)
		
		if ray then
			local hit = ray.Instance
			local model = hit:FindFirstAncestorOfClass("Model")
			
			if model and model ~= player.Character then
				local humanoid = model:FindFirstChildWhichIsA("Humanoid")
				
				if humanoid then
					bullet:Destroy()
					
					if hit.Head then
						humanoid:TakeDamage(45)
					else
						humanoid:TakeDamage(15)
					end
					
					return true
				end
			end
			
			break
		end

		if not bullet.Parent then
			bullet:Destroy()
			
			break
		end
	end

	if bullet then 
		bullet:Destroy() 
	end
end

script.Parent.MouseClicked.OnServerEvent:Connect(fireAmmo)
1 Like

Thanks for your help. My gun is now fixed and “locked and loaded”.

1 Like

No problem, have a great day!
If you need anymore help, feel free to ask.

2 Likes