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.
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.