Raycast Not Detecting Accessories

Hey DevForums,

I’ve been working on a railgun using raycasting. I’ve been trying to make it so if the ray hits an accessory, it’ll make the person wearing it take damage. Unfortunately, it seems to not work. I’ve tried making so if the the ClassName of the object is an accessory, it’ll do damage to the player.

Here is the Script:

local range = 400
local debris = game:GetService("Debris")

local bulletFolder = game.Workspace.Bullets

local function CreateBeam(orgin, direction)
	local midpoint = orgin + direction/2

	local part = Instance.new("Part")
	part.Parent = bulletFolder
	part.Anchored = true
	part.CanCollide = false

	part.Material = Enum.Material.Neon
	part.Transparency = 0.4
	part.BrickColor = BrickColor.new("Brick yellow")

	part.CFrame = CFrame.new(midpoint, orgin)
	part.Size = Vector3.new(.25, .25, direction.magnitude)

	debris:AddItem(part, 0.15)
end

local function muzzle()
	tool.MuzzleFlash.MuzzleEffect.Enabled = true
	tool.MuzzleFlash.SpotLight1.Enabled = true
	tool.MuzzleFlash.SpotLight2.Enabled = true
	tool.MuzzleFlash.SpotLight3.Enabled = true
	tool.MuzzleFlash.SpotLight4.Enabled = true
	tool.MuzzleFlash.SpotLight5.Enabled = true
	wait(0.1)
	tool.MuzzleFlash.MuzzleEffect.Enabled = false
	tool.MuzzleFlash.SpotLight1.Enabled = false
	tool.MuzzleFlash.SpotLight2.Enabled = false
	tool.MuzzleFlash.SpotLight3.Enabled = false
	tool.MuzzleFlash.SpotLight4.Enabled = false
	tool.MuzzleFlash.SpotLight5.Enabled = false
end

local ig = {}

tool.Fire1.OnServerEvent:Connect(function(player, mousePos, orginPos)
	local direction = (mousePos - orginPos).Unit*range
	local result = workspace:Raycast(orginPos, direction)

	if result then
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Humanoid")
		local head = character:FindFirstChild("Head")

		if humanoid and humanoid ~= player.Character.Humanoid then
			if result.Instance.Name == "Head" then
				tool.Handle.Hit:Play()
				humanoid:TakeDamage(170)
			else
				tool.Handle.Hit:Play()
				humanoid:TakeDamage(90)
			end
			if result.Instance.ClassName == "Accessory" then
				tool.Handle.Hit:Play()
				humanoid:TakeDamage(90)
			end
		end
	end	
	tool.Fire.Shoot:Play()
	CreateBeam(orginPos, direction)
	muzzle()
	wait(0.2)
	tool.Fire.Charge:Play()
end)

if a raycast hits an accessory it returns the handle part instead of an accessory

1 Like

I tried doing this but this also doesn’t seem to work.

		if result.Instance.Name == "Handle" then
			tool.Handle.Hit:Play()
			result.Instance.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(85)
		end

Anyway around this?

If u havent tried yet, you can print out the hit to checkout for what u actually have to check

Yeah I’ve tried that. It prints the handle.

Did you put the accessory check under the if humanoid statement?

Yeah, I put the check under the if humanoid statement.

Well, your character variable only checks for the first parent of it meaning in this case would only be the accessory instance, you need to put the accessory check below the result statement which your code would look something like this:

tool.Fire1.OnServerEvent:Connect(function(player, mousePos, orginPos)
	local direction = (mousePos - orginPos).Unit*range
	local result = workspace:Raycast(orginPos, direction)

	if result then
        if result.Instance.Name == "Handle" then
			tool.Handle.Hit:Play()
			result.Instance.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(85)
	        tool.Fire.Shoot:Play()
	        CreateBeam(orginPos, direction)
	        muzzle()
	        wait(0.2)
	        tool.Fire.Charge:Play()
            return
		end
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Humanoid")
		local head = character:FindFirstChild("Head")

		if humanoid and humanoid ~= player.Character.Humanoid then
1 Like