Shooting a Ray from a muzzle to damage

I’m trying to shoot out a ray from a muzzle so I can use it for a VR Weapon but it won’t even return anything here is the code I’ve tried

local Handle = script.Parent.Handle
local muzzle = Handle.Muzzle

local damageEvent = game.ReplicatedStorage:WaitForChild("DamageEvent") -- Replace "DamageEvent" with the actual name of your event in ReplicatedStorage

local UserInputService = game:GetService("UserInputService")

function Fire(input, processed)
	if not processed or not script.Parent.Enabled then return end
	print(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local origin = muzzle.Position
		local direction = muzzle.CFrame.LookVector
		local maxDistance = 100 -- Change this to the desired raycast distance

		local raycastResult = workspace:Raycast(origin, direction * maxDistance)

		if raycastResult then
			local hitPart = raycastResult.Instance
			local humanoid = hitPart.Parent:FindFirstChild("Humanoid")

			if humanoid then
				local damageAmount = 10 -- Replace this with your desired damage amount
				damageEvent:FireServer(humanoid, damageAmount)
				print(hitPart.name)
			end
		end
	end
end


UserInputService.InputBegan:Connect(Fire)

It doesn’t return anything when I shoot a humanoid I’ve even tried shooting above, below, left, right, etc.

1 Like

Because you put if not processed and then return. In most cases, this will return everything immediately. Silly mistake, I know, I’m the master of them. Do if processed.

Hmm, maybe using FindFirstAncesterOfClass to get the character can work.

local Handle = script.Parent.Handle
local muzzle = Handle.Muzzle

local damageEvent = game.ReplicatedStorage:WaitForChild("DamageEvent") -- Replace "DamageEvent" with the actual name of your event in ReplicatedStorage

local UserInputService = game:GetService("UserInputService")

function Fire(input, processed)
	if not processed or not script.Parent.Enabled then return end
	print(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local origin = muzzle.Position
		local direction = muzzle.CFrame.LookVector
		local maxDistance = 100 -- Change this to the desired raycast distance

		local raycastResult = workspace:Raycast(origin, direction * maxDistance)

		if raycastResult then
                        print(raycastResult)
			local hitPart = raycastResult.Instance
                        local model = hitPart:FindFirstAncestorOfClass("Model")
                        if not model then print("Hit object is not in a model") return end
			local humanoid = model:FindFirstChild("Humanoid")

			if humanoid then
				local damageAmount = 10 -- Replace this with your desired damage amount
				damageEvent:FireServer(humanoid, damageAmount)
				print(hitPart.name)
			end
		end
	end
end


UserInputService.InputBegan:Connect(Fire)

You may notice that I printed raycastResult too. Tell me if it prints, if not, the issue is with the ray itself.

(this may be incorrect, im on mobile)

Ok so just do

if processed or not script.Parent.Enabled then
		return 
	end
1 Like

It might be something with processed I tried what you said and it still isn’t printing im gonna test processed though

I did not notice that first line! It probably was that :sweat_smile:

1 Like

I fixed the processing now its working its just not damaging it but i think its something with my model that i gotta fix thank you though!

No problem, I knew it was a silly mistake :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.