In ROBLOX Studio I need to check what part is in front of the model Freddy.

I need to set the variable called thingInFront to the part that is in front of the model Freddy so that I can decide whether I should allow him to walk through or not. This is my current code:

thingInFront = nil
if thingInFront.BrickColor == BrickColor.new('Black') then
    script.Parent.Parent.CanCollide = true
    if not thingInFront.BrickColor == BrickColor.new('Black') then
        script.Parent.Parent.CanCollide = false
  • \$\begingroup\$ Hello, I just removed the greeting, signature, etc. from your post; those are not necessary (your name is on the post already, and we know you're here asking for help), and they actually detract from the important part of the post, which is the question. You can further edit your post or revise my edit. \$\endgroup\$ – Seth Battin Aug 5 '15 at 13:45
  • \$\begingroup\$ And while you're editing, you should include much more detail. You linked to the general page of the engine you are using, but you didn't include any of your code whatsoever except for a couple of variable names. So there is no way anyone can help you unless you include at least some way to look at your current work. \$\endgroup\$ – Seth Battin Aug 5 '15 at 13:47

Just like to say that I have little experience in the lua language


If you take the x y z position of your model "Freddy" and the x y z position of all your other bricks, from there you could find the distance between "Freddy" and the brick and whether the color of that brick is black or not. Then when "Freddy" is a touching distance from the brick, you can allow him to pass through it or not.

Hopefully this helps.


I know this respond is late but for whoever will check this I highly recommend using Dot product aka scalar product (math) to see if the instances is in front of your "Freddy" character. Dot product returns the scalar which represents the length of the vector calculated by taking 2 vectors and the angle between them.

Why ? Dot product is extremely cheap as its a simple calculation. It's also easy to use and fast to set it up.

-- Note

U should be aware of calculating it by using CFrame.LookVector (depending on your case) so it won't compute World position but the relative position to your character.

  • \$\begingroup\$ This answer would be even better if it showed an example of a dot product computation and how we would use its value to determine which object is in front. \$\endgroup\$ – DMGregory May 28 '20 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.