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)
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)
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)
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.
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)
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)