workspace:Raycast not getting a result

I’m making a punching like system and instead of using a touched event I wanna use raycasting. It was working fine before, but now it’s just broken.

local rayOrigin = character.HumanoidRootPart.Position
local rayDirection = character.HumanoidRootPart.CFrame.LookVector * Vector3.new(0, 0, 7)

local params = RaycastParams.new()

params.FilterDescendantsInstances = {character}
params.FilterType = Enum.RaycastFilterType.Blacklist

local rayResult = workspace:Raycast(rayOrigin, rayDirection, params)

if rayResult then

	print("Found result")

	local hit = rayResult.Instance

	if hit.Parent:FindFirstChild("Humanoid") then

		print("Player was hit")
	end
end

It isn’t printing “Found result” so somehow its not finding the raycast result. No errors in the output either.

1 Like

You’re certain there’s something in front of the character when this is called?

Also, it would help if you posted more of the script, and said where you’re putting this script.

If I add local character = script.Parent and put this in StarterCharacterScripts and put a big wall right in front of the spawn location, it’s working fine for me. Printing Found result and everything.

Okay so I’m using UserInputService on the client and firing a remote event. Then on the server this is the whole script

attackEvent.OnServerEvent:Connect(function(player, last)

	local character = player.Character
	local humanoid = character.Humanoid

	local cooldown = false

	local anim = humanoid:LoadAnimation(script:FindFirstChild(last))

	if not anim.IsPlaying then

		anim:Play()
	end

	local rayOrigin = character.HumanoidRootPart.Position
	local rayDirection = character.HumanoidRootPart.CFrame.LookVector * Vector3.new(0, 0, 7)

	local params = RaycastParams.new()

	params.FilterDescendantsInstances = {character}
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(rayOrigin, rayDirection, params)

	if rayResult then

		local hit = rayResult.Instance

		if hit.Parent:FindFirstChild("Humanoid") then

			if not cooldown then

				cooldown = true

				local enemy = hit.Parent

				enemy.Danger.Value = enemy.Danger.Value + 3

				character.Knockback.Value = character.Knockback.Value + 15

				local connect = enemy.Humanoid:LoadAnimation(script.Connect)
				connect:Play()

				local hit = script.Hit:Clone()
				hit.Parent = enemy.HumanoidRootPart
				hit.Enabled = true

				local sounds = game.SoundService.Punches:GetChildren()

				local random = math.random(1, #sounds)

				local hitSound = game.SoundService.Punches:FindFirstChild("Hit"..random):Clone()

				hitSound.Parent = enemy.HumanoidRootPart
				hitSound:Play()

				game:GetService("Debris"):AddItem(hitSound, 1)

				wait(0.2)

				hit.Enabled = false

				wait(0.2)

				hit:Destroy()

				cooldown = false
			end
		end
	end
end)

The original raycasting code you posted worked fine for me, so that’s not the issue.

Try using the debugger to put a breakpoint in your code and step through it line by line and inspect the variables at each step.

Is it possible your event isn’t even running?

This would be extremely rare… But check properties of target if CanQuery = true.
CanQuery is used to make part unraycastable(?).