I'm building a zombie game, and I've been having some trouble with the gun script. I would like for the ray to go straight through the gun and players.

I have a gun with a handle and an invisible tip.

this is in a local script inside the tool:

local Debris = game:GetService("Debris")

local LaserGun = script.Parent
local Tip = LaserGun.Tip

local Player = game.Players.LocalPlayer
local Character = Player.Character

local GunDamage = 30


LaserGun.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
		local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
		
		local LaserBeam = Instance.new("Part", game.Workspace)
		LaserBeam.BrickColor = BrickColor.new("New Yeller")
		LaserBeam.FormFactor = "Custom"
		LaserBeam.Material = "Neon"
		LaserBeam.Transparency = 0.25
		LaserBeam.Anchored = true
		LaserBeam.CanCollide = false
		
		
		local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
		LaserBeam.Size = Vector3.new(0.1, 0.1, LaserDistance)
		LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
		
		Debris:AddItem(LaserBeam, 0.1)
		
		if HitPart then
			local HitHumanoid = HitPart.Parent:FindFirstChild("Zombie")
			
			if not HitHumanoid then
				HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Zombie")
			end
			if HitPart.Parent == Player or LaserGun then
				
			end
			
			if HitHumanoid then
				HitHumanoid:TakeDamage(GunDamage)
			end
		end
	end)
end)

10 comments

1

Raycast filter types exist.

0

Where did you get the script?

0

Could you please clarify this question? See https://idownvotedbecau.se/UnclearQuestion

0

I was wondering how to make a table to filter what parts the ray can hit

0

I'm sorry if I worded it wrong, but I couldn't understand how to create a table to filter what parts a ray can hit

0

Where did you get the script? Can you do research on google before asking? Did you go on the Developer Hub or other resources

0

theres this one thing called getraywithwhitelist or soemthing i rly cant remembre

0

Which just so happens to be deprecated

0

TTChaos I've been trying to solve this issue for days and I don't usually work with rays, of course I've been on all the resources I can find