Ray cast don't work

  1. What do you want to achieve?
    I want to use a raycast to find if player is block by a part in a direction,If not kill the player

  2. What is the issue?
    So a part had a raycast script and when the player is close eg.100 stud it’ll trigger but when it is above that,There is no result of the raycast,Bear in mind that there is actually a part blocking the player,But i’ve add it to the ignore list

  3. What solutions have you tried so far?
    I’ve try delete the part,Which show me error so the ignore list is getting executed but.ANd when i fix the error it’s still don’t detect the part until i move it close.I heard that there is a limit to raycast distance but that’s 5000 stud not 100.

The code uses workspace:RayCast() function which is entirely taken from roblox dev hub and modfied

local rayOrigin = Vector3.new(-215.352, 160.586, -3.802)

for _, v in pairs(game.Players:GetChildren()) do
	local rayDestination = v.Character.Head.Position
	local rayDir = rayDestination - rayOrigin
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDir, raycastParams)
	if raycastResult ~= nil then
		local hitPart = raycastResult.Instance
		if hitPart.Parent:FindFirstChildOfClass("Humanoid") then
			local hum = hitPart.Parent:FindFirstChildOfClass("Humanoid")
			hum.Health = 0
			print("Killed " .. v.Name)

Sincerely tin_nim

Try using .Unit, then multiplying it by the distance + an additional 2 studs or 3 studs


local Dist = (v.Character.Head.Position-Origin).Magnitude
local rayDir = (rayDestination-rayOrigin).Unit*Dist+1

What does this is make the ray as long as the distance between the player and the origin is, plus another 1 to 3 studs to account for possible accessories

Or another problem could also or actually be that the ray hits the player’s accessories instead of the bodyparts. What you could do for this is insert all the player’s body parts in the filterDescendants, then set the filter type to white list so it ignores everything except the body parts