Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is wrong with this onTouched script involving meshes and parts?

What is wrong with this onTouched script? It's a brick (HitBox) hitting a mesh named B2, and I'm checking in output and all I see is "It isn't B2, It is B2!", which obviously is contradicting. So something is wrong and I can't seem to name it. B2 is in a model named Car, and the script is in HitBox.

local B2 = script.Parent.Parent.Parent.Car.B2
local HitBox = script.Parent

HitBox.Touched:Connect(function(Part)
	if Part.Name == B2 then
		print("It is B2!")
script.Parent.Parent.B3.Transparency = 0
script.Parent.Parent.B3.CanCollide = true

script.Parent.Parent.B1.Transparency = 1
script.Parent.Parent.B1.CanCollide = false
else
      print("It isn't B2, it is ".. Part.Name .."!")
      end
end)

2 comments

0

Try if Part.Name == 'B2' ?

0

I'm so dumb, should've thought of that. Thank you so much!

1 answer

✓ Accepted answer

line 5:

if Part.Name == B2 then

you are comparing incompatible types: Instance != String

there seem to be two solutions, one to compare the name, and one to compare the exact part with the touched one:

SOLUTION 1: if Part.Name == "B2" then

mind that there are "s surrounding the B2

SOLUTION 2: if Part == B2 then

This discussion is preserved as it was. New answers, comments, and votes are closed.