Super simple raycast not working

Raycast should shoot towards player to check if monster has line of sight, then kill them, but doesn’t see player (or any instance for that matter. When I tried checking for an instance it threw an error)

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local Character = hit.Parent
		local Params = RaycastParams.new()
		
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params.IgnoreWater = true
		Params.FilterDescendantsInstances = script.Parent:GetDescendants()
		
		local Raycast = workspace:Raycast(script.Parent.Position, (Character.HumanoidRootPart.Position - script.Parent.Position) * 200, Params)

		
		if Raycast then
			print("ya!")
			if game.Players:GetPlayerFromCharacter(Raycast.Instance.Parent) then
				print("ja!")
				Raycast.Instance.Parent.Humanoid.Health = 0
			end
		end
	end
end)

Edit: Video of the problem (although probably unnecessary)

5 years of programming to be defeated by this…
Any help is appreciated!

Is the Raycast colliding with an accessory handle inside of your character, rendering this line:
if game.Players:GetPlayerFromCharacter(Raycast.Instance.Parent) then
to return false?

I don’t know if this was actually the problem or not but I enabled “respect cancollide” and that seemed to fix it, so it was likely hitting the cancollide off accessories. Thanks!

1 Like

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