You can write your topic however you want, but you need to answer these questions:
I am making a platofrm occupancy script that uses :GetPartsInPart then checks if the part thats inside it name is Sensor or Sensor B
It manages to change the boolvalue to true but once the sensors leave the part it doesnt rollback to false
while task.wait() do
for i, v in pairs(workspace:GetPartsInPart(script.Parent.Sensors.VoieA)) do
if v.Name == "Sensor" or "SensorB" then
script.Parent.VoieAOcc.Value = true
else
script.Parent.VoieAOcc.Value = false
end
end
for i, v in pairs(workspace:GetPartsInPart(script.Parent.Sensors.VoieB)) do
if v.Name == "Sensor" or "SensorB" then
script.Parent.VoieBOcc.Value = true
else
script.Parent.VoieBOcc.Value = false
end
end
for i, v in pairs(workspace:GetPartsInPart(script.Parent.Sensors.VoieC)) do
if v.Name == "Sensor" or "SensorB" then
script.Parent.VoieCOcc.Value = true
else
script.Parent.VoieCOcc.Value = false
end
end
end
When there are no parts inside the area, then that “for” cycle wont have anything to check the name of, and because of that the if statement that changes the value will also not run.
Also, if you have the part “Sensor” or “SensorB” inside and some other parts, the following problem could occour:
Let’s say you have 3 parts of name: “PartA”, “PartB” and “Sensor”, all inside the area.
The code could, in this order:
- Check PartA’s name → Value = false;
- Check PartB’s name → Value = false;
- Check Sensor’s name → Value = true;
Which is fine, but it could instead do
- Check PartA’s name → Value = false;
- Check Sensor’s name → Value = true;
- Check PartB’s name → Value = false;
Which gives the wrong result.
This is what I would write:
while task.wait() do
local partsInVoieA = workspace:GetPartsInPart(script.Parent.Sensors.VoieA)
if #partsInVoieA == 0 then
script.Parent.VoieAOcc.Value = false
else
for i, v in partsInVoieA do
if v.Name == "Sensor" or "SensorB" then
script.Parent.VoieAOcc.Value = true
break
else
script.Parent.VoieAOcc.Value = false
end
end
end
-- copy and paste the above and change to VoieB and VoieC
end
Hope this helps!
if v.Name == "Sensor" or "SensorB" then
People always seem to do this mistake from time to time, lol.
When you do if a == b or c then, the program isn’t reading it as aight, is a that b thing or c thing? Instead it’s reading it as aight, does a match b or is c truthy? You could also read it as if (a == b) or (c) then.
The solution to this is to check the name for both, for example if a == b or a == c then so that it reads if (a == b) or (a == c) then, or in the context of your program if v.Name == "Sensor" or v.Name == "SensorB" then.
A simpler solution would be to simply use a dictionary though.
local validName = {
["Sensor"] = true,
["SensorB"] = true
}
...
local enableA = false
for i, v in pairs(workspace:GetPartsInPart(script.Parent.Sensors.VoieA)) do
if validName[v.Name] ~= nil then
enableA = true
break
end
end
script.Parent.VoieAOcc.Value = enableA
Applying these changes should fix your program. 