I want a list of parts touching my main part
the parts that will be touching and need to be detected will have collisions on.
The main part does not have collisions on.
This script is put inside the parts I want to detect touching parts in
local Sea = script.Parent
local Workspace = game:GetService("Workspace")
local Touching = {}
local Overlaps = OverlapParams.new()
print("run")
while true do
task.wait()
TouchingNow = Workspace:GetPartBoundsInBox(Sea.CFrame, Sea.Size, Overlaps)
-- Cleans up past Floaties
for _,Toucher in pairs(Touching) do
if not table.find(Toucher, TouchingNow) then
Toucher.Floatie:Destroy()
print("Cleaned")
end
end
-- Creates new floaties
for _,Toucher in pairs(TouchingNow) do
if not Toucher:IsDescendantOf(Workspace.Boats) then
return
end
if not table.find(Toucher, Touching) then
local Floatie = script.Floatie:Clone()
Floatie.Parent = Toucher
Floatie.Attachment0 = Toucher:FindFirstChild("FloatPoint") or Toucher:FindFirstChildOfClass("Attachment")
print("FloatingWork")
local YDistance = (Toucher.Position - Vector3.new(Toucher.Position.X, Sea.Position.Y + (Sea.Size.Y / 2), Toucher.Position.Z)).Magnitude
Floatie.Force = Vector3.new(0, YDistance * 2, 0)
end
end
-- Updates Floaties
for _,Toucher in pairs(TouchingNow) do
print("Working")
if not Toucher:IsDescendantOf(Workspace.Boats) then
return
end
print("Floatwork")
local Floatie = Toucher.Floatie
local YDistance = (Toucher.Position - Vector3.new(Toucher.Position.X, Sea.Position.Y + (Sea.Size.Y / 2), Toucher.Position.Z)).Magnitude
Floatie.Force = Vector3.new(0, YDistance * 2, 0)
end
Touching = TouchingNow
end
if you want just touching the part then you have to use GetTouchingParts() this returns part that are touching not inside the part
if you want all the parts that are colliding with the part then use GetPartsInPart()
if these dont return anything you could always set the main part to be collidable and touchable then run the function, then instantly turn it off. during that time nothing will collide with it
now this doesnt have anything to do with what you want but i see you are trying to make some sort of way to make stuff float on the water. the issue with this is detecting collisions between parts is very slow and unreliable for this kind of thing. you should use a Y check for this instead. this will be much more reliable
for example:
if y <= 0 then
float
end
also using a while true with task.wait is bad for something to do with physics. you should always use RunServicr.Heartbeat. this runs every physics tick and will be synced with everything else
just my suggestions to improve what you are trying to do
You can use this GetTouchingParts method which behaves similarly to the normal one but it works with parts that don’t have collision on as well
local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
TouchingNow = GetTouchingParts(Sea) -- returns parts touching "Sea"