Elevator TP using Region3 not working

  1. What do you want to achieve?
    I have a elevator which TPs a player to another elevator by detecting who is inside via Region3.

  2. What is the issue?
    The Region only teleports one player if there are two in the elevator.

  3. What solutions have you tried so far?
    No solutions.

	local ignore = {}
	ignore[1] = from
	ignore[2] = to
	
	local zone = from:WaitForChild("Area")
	local region = Region3.new(zone.Position - zone.Size/2, zone.Position + zone.Size/2)
	local parts = workspace:FindPartsInRegion3WithIgnoreList(region, ignore)
	
	for _, part in pairs(parts) do
		if part:IsA("Accessory") or part:IsA("Accoutrement") then return end
		print(part.Name)
			if part.Name == "HumanoidRootPart" or part.Name == "ItemHandle" then
			 part.Parent:SetPrimaryPartCFrame(to.Area.CFrame * from.Area.CFrame:ToObjectSpace(part.CFrame))
		end
	end

Instead of using return , you can use a boolean flag to indicate whether to process the part or not

Example of boolean:

local ignore = {}
ignore[1] = from
ignore[2] = to

local zone = from:WaitForChild("Area")
local region = Region3.new(zone.Position - zone.Size/2, zone.Position + zone.Size/2)
local parts = workspace:FindPartsInRegion3WithIgnoreList(region, ignore)

for _, part in pairs(parts) do
    local processPart = true

    if part:IsA("Accessory") or part:IsA("Accoutrement") then
        processPart = false
    end

    if processPart then
        print(part.Name)
        if part.Name == "HumanoidRootPart" or part.Name == "ItemHandle" then
            part.Parent:SetPrimaryPartCFrame(to.Area.CFrame * from.Area.CFrame:ToObjectSpace(part.CFrame))
        end
    end
end

the return statement was the cause of the issue. When a return statement is encountered within a loop, it causes the loop to exit prematurely. In your original script, if a part is an “Accessory” or “Accoutrement”, the return statement is executed, and the loop exits without checking the remaining parts. As a result, if there are two players in the elevator, only one player is teleported.

2 Likes

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