Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

How do I make this raycast ignore tools that are being held?

Asked by
Narroby 10
9 years ago
        local ray =, (mouse.Hit.p -  tool.Handle.CFrame.p).unit*30)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)

        local torso = hit and hit.Parent and hit.Parent:FindFirstChild("Torso")
        if torso then
            torso.CFrame =, 3, 69))

Please keep in mind I have very basic scripting knowledge.


So, I was playing around with a raycasting tutorial, and i made a pretty much working teleporter gun. After testing it a bit, I noticed that tools that players were holding were not ignored, so I opened up the script and prepared to add it to the exception list.

I was planning on adding a check that, if what the raycast hit was a Tool, it'd be ignored, but I ran into a pretty dumb problem.

local hit, position = game.Workspace:FindPartOnRay(ray, user)

Putting the variable for the tool inside the function parameters would be the way to go - but to check if it's a tool, I need to use the hit variable (to check if what it hit is a tool).

Which is, of course, impossible, because the hit variable is being defined where I want to use the hit variable.

I don't even know if I'm making sense right now, but how can I make this work?

And if this isn't the way to make the raycast ignore tools, then what's the correct way?

Though I have little experience with raycasting, I recommend using FindPartOnRayWithIgnoreList: aquathorn321 858 — 9y

1 answer

Log in to vote
Answered by 9 years ago

"if what the raycast hit was a Tool, it'd be ignored"

Why not just have it ignore all tools in the first place? In fact, if the tools you wish to ignore are all children of the player's character, then your code should work fine as is (assuming "user" refers to the player's character in the workspace instead of the player object in game.Players). If the tools are lying around the workspace (not in the player's character), then you'll need to create a loop:

local hit, position
local ignore = {user}
    hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore)
    if hit ~= nil and hit.ClassName == "Tool" then
        table.insert(ignore, hit)
until hit == nil or hit.ClassName ~= "Tool"

Performance-wise, it would be better to just maintain a list of tools to ignore rather than raycasting until you don't get a tool.


Answer this question