So I wrote this script last night, and I added a check to make sure only specifically named models could trigger the script. The checks don’t work and I’d like some help figuring out why as the person who I wrote this script for is not a scripter and cannot help me. I’d also like to add a cooldown to the script so that it can only happen once every 30 seconds if possible.
The script is as follows below
local SensorPart = script.Parent
local Switches = game.Workspace.Switches.Cadman_Place
local Diverge = Switches.CP_D_A
local Straight = Switches.CP_S_A
local iconLCL = Switches.iconLCL.SurfaceGui.Frame
local iconEXP = Switches.iconEXP.SurfaceGui.Frame
function Sensor(otherPart)
if otherPart.ClassName == "Model" and otherPart.Name == "Bombardier R62A (FL)" or "Bombardier R179 (8)" or "Kawasaki R142A" or "Kawasaki R62 (FL)" then
for i,v in pairs (Straight:GetChildren()) do
if v:IsA("MeshPart") and v.Name == 'Rail' then
v.Transparency = 0.5
v.CanCollide = false
end
end
for i,v in pairs (Diverge:GetDescendants()) do
if v:IsA("MeshPart") and v.Name == 'Rail' then
v.Transparency = 0
v.CanCollide = true
end
end
iconEXP.Arrow.Image = "rbxassetid://10204472357"
iconEXP.Status.Text = "STOP"
iconEXP.Status.TextColor3 = Color3.new(255,0,0)
iconEXP.Cooldown.TextTransparency = 0
iconLCL.Arrow.Image = "http://www.roblox.com/asset/?id=6776872133"
iconLCL.Arrow.Rotation = -135
iconLCL.Status.Text = "GO"
iconLCL.Status.TextColor3 = Color3.new(0,255,0)
iconLCL.Cooldown.TextTransparency = 0
else print("TEST MESSAGE: THIS MEANS THE SCRIPT WORKS") end
end
SensorPart.Touched:Connect(Sensor)
I’d say you should make a table to store the model names as string values like so:
local thisTable = {
"Bombardier R62A (FL)",
"Bombardier R179 (8)",
"Kawasaki R142A"
-- add more afterwards
}
And in your script loop through this table to check if the model that touched this sensor is the desired model. Obviously there’s individual parts any model and when it comes to models and the Touched event it usually returns the specific part within the model for the otherPart parameter, which is why I write otherPart.Parent to reference the entire model, and also because of doing this habitually from experience in detecting whether a player’s character touches something.
function Sensor(otherPart)
for i, v in pairs(thisTable) do
if otherPart.Parent:IsA("Model") and otherPart.Parent.Name == v then
-- executable code goes here, model found via name
else
return -- not a model, or a model specifically named in the table
end
end
end
And for a delay you can initialize a boolean set to be false and having a conditional statement based on the boolean like so:
local debounce = false
function Sensor(otherPart)
if debounce == false then
debounce = true
for i, v in pairs(thisTable) do
if otherPart.Parent:IsA("Model") and otherPart.Parent.Name == v then
-- executable code goes here, model found via name
else
return -- not a model, or a model specifically named in the table
end
end
wait(30)
debounce = false
end
end
SensorPart.Touched:Connect(Sensor)
Hello,
I tested this and forgot to reply, however for some reason the script doesn’t run when touched by the model and it doesn’t give any errors oddly enough.
local SensorPart = script.Parent
local thisTable = {
"Bombardier R62A (FL)",
"Bombardier R179 (8)",
"Kawasaki R142A"
-- add more afterwards
}
local debounce = false
function Sensor(otherPart)
print(otherPart)
if debounce == false then
debounce = true
print(otherPart)
for i, v in pairs(thisTable) do
if otherPart.Parent:IsA("Model") and otherPart.Parent.Name == v then
print(v)
-- executable code goes here, model found via name
else
return -- not a model, or a model specifically named in the table
end
end
wait(30)
debounce = false
end
end
SensorPart.Touched:Connect(Sensor)
Is Trig the part that is consistently and the part that you want interacting with your sensor? Because if so try changing the conditional statement to this due to the structure of your heierarchy:
if otherPart.Parent.Parent:IsA("Model") and otherPart.Parent.Parent.Name == v then
The folder Values parent is the model you are looking for.
If Trig is not the part you want interacting with the sensor then try going further up the hierarchy with your desired part that you want interacting with it:
True but maybe :IsDescendantOf() could work in this scenario if all the specified models are supposedly in workspace all the time, or in some organized folder?