Essentially, I want to find 1 or 2 items in a table and determine whether it belongs to a model, then return true or false back to client, so it can do whatever.
I’ve tried for loops, but because it’s a loop, it will return true and immediately return false because it’ll inevitably find one that doesn’t belong to that model → this is only because the remote function will be in a while loop that repeats every second or so, which makes this far from ideal.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage.RemoteFunction
local partsInRegion = {}
remoteFunction.OnServerInvoke = function(train, stationFolder)
if (train and stationFolder) then
if workspace.ActiveTrains:FindFirstChild(train.Name) then
if workspace.StationSensors:FindFirstChild(stationFolder.Name) then
local stationSensor = workspace.StationSensors[stationFolder.Name]:FindFirstChild("StationRegion")
if stationSensor then
for _, v in pairs(workspace:GetPartsInPart(stationSensor)) do -- I'm pretty sure this is redundant, because :GetPartsInPart returns a table -_-
if not table.find(partsInRegion, v.Name) then
table.insert(partsInRegion, v.Name)
end
end
if #partsInRegion > 0 then
else
return false
end
else
return false
end
else
return false
end
else
return false
end
else
return false
end
end
Whats the “model” in this script?
(also, added tip, remove all of those else statements and return false’s and put return false just before end) to clean it up)
All in all, when the train enters the platform, I want this script to detect its fully in the platform before allowing it to load passengers (open doors).
Maybe check the parts name if it’s any of those names, then use FindFirstAncestorOfClass to get the model from that part and whatever validation method of your choice for that model.
Tell me what this prints: (I cleaned the script up a bit and added a possibility for return true)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage.RemoteFunction
local newOverlapParams = OverlapParams.new()
newOverlapParams.FilterType = Enum.RaycastFilterType.Include
remoteFunction.OnServerInvoke = function(train, stationFolder)
if not train or not stationFolder or not workspace.ActiveTrains:FindFirstChild(train.Name) then
return
end
local stationSensors = workspace.StationSensors:FindFirstChild(stationFolder.Name)
if not stationSensors then
return
end
local stationSensor = stationSensors:FindFirstChild("StationRegion")
if not stationSensor then
return
end
newOverlapParams.FilterDescendantsInstances = {train}
local partsInPart = workspace:GetPartsInPart(stationSensor, newOverlapParams)
print(partsInPart)
for i, descendant in ipairs(train:GetDescendants()) do
if descendant:IsA("BasePart") and descendant.CanQuery and not table.find(partsInPart, descendant) then
return
end
end
print(partsInPart)
return true
end
I’d love to try your script today, but might have to hold it off until tomorrow. because Roblox is being a bum and not working. Nevermind! Roblox started working, but it doesn’t print anything, I’m guessing it’s returning at something, I’ll continue tomorrow.
Just a question, what is descendant:IsA("BasePart") meant to prevent, because it does keep returning at that point. It worked! I performed some tweaks! Thank you so so much!