Gun is able to shoot player

Hi there, my gun is able to shoot you, therefore damaging yourself. How can I prevent this?

local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local direction = (position - origin).Unit*300
	local result = Workspace:Raycast(origin, direction)

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude

	local bullet_clone = ServerStorage.Bullet:Clone()
	bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
	bullet_clone.Parent = Workspace

	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(10)
		end
	end

	wait(0.1)
	bullet_clone:Destroy()
end)

Not my code, used to a tutorial so I could use raycasting.

Add a blacklist to the Raycast using Raycast Parameters.

local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local direction = (position - origin).Unit*300
	local rayparams = RaycastParams.new()
	rayparams.FilterDescendantsInstances = {player.Character}
	rayparams.FilterType = Enum.RaycastFilterType.Blacklist

	local result = Workspace:Raycast(origin, direction, rayparams)

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude

	local bullet_clone = ServerStorage.Bullet:Clone()
	bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
	bullet_clone.Parent = Workspace

	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(10)
		end
	end

	wait(0.1)
	bullet_clone:Destroy()
end)
1 Like

Just a question if you could answer, do you know how i can add wait-time between firing, i’ve tried using
shooting = false, that sort of thing in the code

cursor.Button1Down:Connect(function()
	shooting = false
	remote:FireServer(cursor.Hit.Position)
	AnimationTrackShotgunWalk:Stop()
	AnimationTrackShotgunIdle:Stop()
	AnimationTrackShotgunShoot:Play()
	wait(0.4)
	AnimationTrackShotgunIdle:Play()
	wait(0.5)
	shooting = true
end)

Though to no usage.

you need to add a check for this.

cursor.Button1Down:Connect(function()
	if shooting then
		shooting = false
		remote:FireServer(cursor.Hit.Position)
		AnimationTrackShotgunWalk:Stop()
		AnimationTrackShotgunIdle:Stop()
		AnimationTrackShotgunShoot:Play()
		wait(0.4)
		AnimationTrackShotgunIdle:Play()
		wait(0.5)
		shooting = true
	end
end)
1 Like