I have items in a table and I want to see if they belong to a model, how do I do this?

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.

Could someone please help?

2 Likes

Maybe set a boolean attribute to the root model (main model) called “Root”, and edit the loop to check for this attribute.

I’ll try this, but wouldn’t it still cause the same problem I listed above?

Sorry, didnt read correctly, can you send your code?

Sure, it’s very incomplete…

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
1 Like

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)

What are you trying to achieve? There is probably a better way to do this.

It is a model of a train, it has 2 parts called “FrontSensor” and “RearSensor”

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.

Might try this since it might potentially work…

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
3 Likes

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!

1 Like

No problem. If you have any questions about how it works, feel free to ask.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.