RayCast laser not working, what's going on?

I’m not really used to RayCasts (or this website) so sorry if I do something wrong, but I want to make a part (Finder) fire out a block called Baby (Finder.Baby) and the script is Finder.Newscript. I tried tinkering with others scripts (and writing my own stuff of course) but I couldn’t seem to get it to work.
My script is:

local Finder = script.Parent
local Baby = Finder.Baby
local Connections = {}
while true do
	print("hi")
	Baby.CFrame = Finder.CFrame
	while (Finder.Position - Baby.Position).Magnitude < 50 do
		Connections[Baby] = Baby.Touched:Connect(function(Hit)
			function Ontouched(hit)
			end
			Baby.CFrame = Baby.CFrame + Baby.CFrame.lookVector * 1
		end)
		wait()
	end
end

function Ontouched (hit)
	if hit ~= workspace.Finder then
		Baby.CFrame = Finder.CFrame
		local culprit = hit
		print("found")
		if culprit.Parent:FindFirstChild("Pants") then
			culprit.Parent.Humanoid.Health = 0
		end
	end
end

When the Baby touches a player, it’s supposed to kill the player then return to the Finder, but the Baby just sits at the start point and I don’t die when walking through its path. Any tips are appreciated!

oh boy where to begin…
first of all while true do loop does not have any wait so if your magnitude is greater than 50 it will literally crash your game.
second you are creating a lot of useless connections since its under a while loop
third you are creating ontouched function inside the .Touched:connect (idk why)
lastly you aren’t even using any raycasting… just ontouched functions

This is made under the assumption that your function works the way it’s intended to and variables are set up nicely
Idk why youd put 2 while waits but I’m 100% sure theres a better way to set this up but this should work somewhat.
There will most likely some other issues you will encounter in the future for this purpose of just detecting the baby/finder should work.

local Finder = script.Parent
local Baby = Finder.Baby
local Connections = {}

local function Ontouched (hit)
	if hit ~= workspace.Finder then
		Baby.CFrame = Finder.CFrame
		local culprit = hit
		print("found")
		if culprit.Parent:FindFirstChild("Pants") then
			culprit.Parent.Humanoid.Health = 0
		end
	end
end

while true do
	Baby.CFrame = Finder.CFrame
	while (Finder.Position - Baby.Position).Magnitude < 50 do
        if not Connections[Baby] then --Since we don't want to keep on making connections
		    Connections[Baby] = Baby.Touched:Connect(function(Hit)
			    Ontouched(Hit); --This should call the function above;
			    Baby.CFrame = Baby.CFrame + Baby.CFrame.lookVector * 1 --also im pretty sure this is gonna cause some issues later on... (for instance moving the baby even after death/constant calling of ontouched/etc)
		    end)
	    end
        wait()
    end
    --Baby is no longer near the finder; so disconnect it;
    if Connections[Baby] then
       Connections[Baby]:Disconnect();
       Connections[Baby] = nil;
    end
    wait()
end
1 Like

Yeah! I looked at more tutorials and realized Roblox has its own automatic ray system. I thought you had to do it manually so I was mixing their rays with physical rays. Thanks!