Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Exclude parts from GetTouchingParts()? (Like Baseplate)

I really didn't want to have to ask one question pretty much right after another, however I looked it up and I wasn't getting answers from the internet and I just couldn't figure it out on my own, however I'm using GetTouchingParts() to make a barrier which teleports touching parts away from it.

barrier = script.Parent

barrier.Touched:Connect(function() end)

while game:GetService("RunService").Stepped:Wait() do
	for _,v in pairs(barrier:GetTouchingParts()) do
		local h = v.Parent
		if h == nil or v.Name == "Baseplate" then return end
		if h == workspace then
			v.CFrame = CFrame.new(43, 21.75, -12)
		else
			local part = h:findFirstChildOfClass("Part")
			if part ~= nil then
				part.CFrame = CFrame.new(43, 21.75, -12)
			end --Run Code with v being the parts touching.
		end
	end
end

The issue is the barrier part is in a model with another part which I really need to get excluded from the above code, which I can't find a way to do. With the Baseplate (another part I want excluded) I had it return end but that means if the barrier touches the Baseplate it stops working entirely.

I resolved this by having the Barrier in the air, so that it's not touching the Baseplate. However, that won't do for the part it shares a model with, because that part is its outline and thus will always touch it, causing it to not work at all.

1 answer

✓ Accepted answer

Actually really simple: just create an if statement, that checks if the part´s name matches Baseplate like this:

barrier = script.Parent

barrier.Touched:Connect(function() end)

while game:GetService("RunService").Stepped:Wait() do
    for _,v in pairs(barrier:GetTouchingParts()) do
	if v.Name ~= "Baseplate" then --the if statement i talked from
	        local h = v.Parent
	        if h == nil or v.Name == "Baseplate" then return end
	        if h == workspace then
	            v.CFrame = CFrame.new(43, 21.75, -12)
	        else
	            local part = h:findFirstChildOfClass("Part")
	            if part ~= nil then
	                part.CFrame = CFrame.new(43, 21.75, -12)
	            end --Run Code with v being the parts touching.
	        end
	    end
	end
end

1 comment

0

Yeah, I'm sorry about that. I'm not even beginner level at lua. Thanks for your help.

This discussion is preserved as it was. New answers, comments, and votes are closed.