So I made a script for the gun, and for the most part, it works, it shoots towards the mouse cursor, and the bullets deal damage to other players. But the problem is that it can shoot through walls, and the bullets also damage the player using the gun. I tried casting a ray with an ignore list, and put the player and the handle in the list, but it still doesn't work.
Here is the script
local DontCastRay = game.ReplicatedStorage:WaitForChild("DontCastRay")
local gun = script.Parent
DontCastRay.OnServerEvent:Connect(function(player, mouse_Hit)
-- Convenience
local gunDist = 70
local gunDmg = 5
local target = nil
-- Find humanoid
local detectRay = Ray.new(gun.Handle.CFrame.p, (mouse_Hit.p - gun.Handle.CFrame.p).Unit * gunDist)
local obj, position = workspace:FindPartOnRayWithIgnoreList(detectRay, {player.Character, gun.Handle, workspace.Part})
if obj:FindFirstChild("Humanoid") then
local humanoid = obj:FindFirstChild("Humanoid")
target = humanoid
local targetIsHuman = true
end
-- Create bullet
if target == target then
local humanoid = target
local bullet = Instance.new("Part", workspace)
bullet.Anchored = false
bullet.Shape = "Ball"
bullet.BrickColor = BrickColor.new("Gold")
bullet.CanCollide = false
bullet.Size = Vector3.new(.2, .2, .2)
bullet.CFrame = CFrame.new(gun.Handle.CFrame.p, mouse_Hit.p)
local vel = Instance.new("BodyVelocity", bullet)
vel.Velocity = bullet.CFrame.LookVector * gunDist
bullet.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
humanoid.Health = humanoid.Health - gunDmg
end)
game:GetService("Debris"):AddItem(bullet, 1.5)
end
end)
Any help would be appreciated
