How can I get the MODELS name instead of the parts that hit the object?

Hello,

So i’ve made a script that when the sensor detects an specific name its going to do somehting.
But the problem is that its not printing the name of the model but the parts that hit the sensor. And one more thing I don’t get is that when I put "if hit.Name == “FerrariSF90"” or “Ferrari458” its still printing the names that got hit. Even though I placed “print” after the line above ^.

Can someone help me with this?

Here’s the script;

local Ferrari = game.Workspace.Ferrari
local FerrariBrands = {
	"FerrariSF90";
	"Ferrari458";
}
print(FerrariBrands)
local sensor = script.Parent

sensor.Touched:Connect(function(hit)
	
	print("ok")
	if hit.Name == "FerrariSF90" or "Ferrari458" then
		print(hit.Name)
		
	end
end)
2 Likes
local Ferrari = game.Workspace.Ferrari
local FerrariBrands = {
	"FerrariSF90";
	"Ferrari458";
}
print(FerrariBrands)
local sensor = script.Parent

sensor.Touched:Connect(function(hit)
	
	print("ok")
	if hit.Parent.Name == "FerrariSF90" or "Ferrari458" then
		print(hit.Parent.Name)
		
	end
end)

Maybe try this.

1 Like

I did this;

pritn(hit.Parent.Parent.Name) 

And that worked.

But even though I changed the names its still printing their parent. And that shouldn’t happen, any idea how I can fix that?

1 Like
local Ferrari = game.Workspace.Ferrari

local FerrariBrands = {

"FerrariSF90";

"Ferrari458";

}

print(FerrariBrands)

local sensor = script.Parent

sensor.Touched:Connect(function(hit)

print("ok")

local TouchingPart = hit.Parent

if TouchingPart.Name == "FerrariSF90" or TouchingPart.Name == "Ferrari458" then

print(TouchingPart)

end

end)

Can you try this script?

1 Like

image
Are you sure about this if statement? Did you mean this instead:

if (hit.Name == "FerrariSF90") or (hit.Name == "Ferrari458") then

The original if statement you used translates to: if <boolean> or <string> then, which will ALWAYS return true no matter what, because if the boolean is false it will look towards the string, which is considered true by the if statement. So the print() function will run no matter what.

2 Likes

If I do that then it is only printing “ok” so there must be something wrong with the name or something like that.

I removed the or and the “Ferrari458” but still the same problem.

1 Like

You should use FindFirstAncestorOfClass to get the model, keep in mind that, if the part is inside a model and that model is inside another, it will only take the first model, also, if the name must be inside the table, use table.find

local Ferrari = game.Workspace.Ferrari
local FerrariBrands = {
	"FerrariSF90";
	"Ferrari458";
}
print(FerrariBrands)
local sensor = script.Parent

sensor.Touched:Connect(function(hit)
	print("ok")
	local Model = hit:FindFirstAncestorOfClass("Model")
	if Model and table.find(FerrariBrands, Model.Name) then
		print(Model.Name)

	end
end)

Sadly this doesn’t works either.

mind showing us a screenshot of your explorer hierarchy so we know exactly how the vehicles are formulated?

This is a free model just to test it but here it is;

That explains why you need to do hit.Parent.Parent in order to get the name. The parts are in a model, which itself is a child of the actual vehicle with the correct name.

It also explains why @SOTR654’s solution didn’t work, because it’s looking for the first ancestor that’s a model, and the first one isn’t the actual vehicle.

Try this:

local Ferrari = game.Workspace.Ferrari
local FerrariBrands = {
	"FerrariSF90",
	"Ferrari458",
}
print(FerrariBrands)
local sensor = script.Parent

sensor.Touched:Connect(function(hit)
	print("ok")
	local vehicle
	for _, v in pairs(FerrariBrands) do
		if hit:FindFirstAncestor(v) then
			vehicle = v
			break
		end
	end
	if vehicle then
		print(vehicle)
	end
end)