Raycast Won't Kill Player

Hello, I made a raycast to kill players. But it won’t work, no errors. Code:

local part = game.Workspace.Part
local rayCastParams = RaycastParams.new()

rayCastParams.FilterDescendantsInstances = {game.Workspace.Baseplate, part}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist


local ray = game.Workspace:Raycast(part.Position, Vector3.new(0, 1, 0) * 50, rayCastParams)



while wait(1) do
	if ray then
		local hum = ray.Instance.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(100)
		end
	end
end

I used print statements and it showed that the problem was the code in the while loop. If you have any idea of what’s going on, please tell me. Thanks! :wave:

You need to be casting the ray each time inside the while loop

3 Likes

You are casting the ray once at the beginning of your code only, so just move the local ray ... into the while loop.

local rayCastParams = RaycastParams.new()

rayCastParams.FilterDescendantsInstances = {game.Workspace.Baseplate, part}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist





while wait(1) do
local ray = game.Workspace:Raycast(part.Position, Vector3.new(0, 1, 0) * 50, rayCastParams)
	if ray then
		local hum = ray.Instance.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(100)
		end
	end
end

Try this.

1 Like

It worked once, but then it stopped working. No errors.

Like it killed me once, then when I tried it, it wont kill me.

Try waiting less than 1 second, it seems to work fine for me

1 Like

Are you using the code that @TheAxisCode posted?

No, I just put the ray in the loop.

Let me see the code you are using.

local part = game.Workspace.Part
local rayCastParams = RaycastParams.new()

rayCastParams.FilterDescendantsInstances = {game.Workspace.Baseplate, part}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist






while wait() do
	local ray = game.Workspace:Raycast(part.Position, Vector3.new(0, 0, 1) * 50, rayCastParams)
	if ray then
		local hum = ray.Instance.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(100)
		end
	end
end

Somehow won’t work. I changed wait(1) to wait()

Youre casting it to the side now, change the direction to Vector3.new(0, 1, 0) if you want it to cast upwards

1 Like

Yeah I want to cast the ray forward

Vector3.new

You wrote Vetor3. It will error.

No, it’s already Vector3.new()

Try running around the part, your direction is in world space coordinates. If you want the direction to be relative to the part you should use part.CFrame.LookVector instead of Vector3.new(0, 0, 1)

1 Like

I was talking to @heII_ish, mate.

local part = game.Workspace.Part
local rayCastParams = RaycastParams.new()

rayCastParams.FilterDescendantsInstances = {game.Workspace.Baseplate, part}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

while wait() do
	local ray = game.Workspace:Raycast(part.Position, part.CFrame.LookVector * 50, rayCastParams)
	if ray then
    	print("Object/terrain hit:", raycastResult.Instance:GetFullName())
    	print("Hit position:", raycastResult.Position)
    	print("Surface normal at the point of intersection:", raycastResult.Normal)
    	print("Material hit:", raycastResult.Material.Name)

		local hum = ray.Instance.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(100)
		end
    else
        print("No result found")
	end
end

CFrame.LookVector is a Vector3 value, you can use that

3 Likes