I want this script to work only when one or more of Char's objects collide with one or more of Surface's objects. It would also be useful to have a table (SurfCol) of the objects that are colliding specifically in the Surface table. How can I do this efficiently?
-- Settings --
local GlobalFrequency = 10
while true do
local TickA = elapsedTime()
wait(1/GlobalFreqency)
local Surface = game.Workspace.ImprintableMap:GetChildren()
local Char = game.Workspace:GetChildren()
for i=1,#Char do
if not Char[i]:FindFirstChild("Humanoid") then
table.remove(Char, i)
else
print("Found NPC " .. Char[i].Name)
end
end
for i=1,#Surface do
for l=1,#Char do
local DetectDistance[l] = (Surface[i].position - Char[l].HumanoidRootPart.Position).magnitude
end
local DetectDistance = math.min(DetectDistance)
if DetectDistance >= (Surface[i].Size.X + Surface[i].Size.Z)/2 + 5 then
table.remove(Surface[i])
end
end
if DetectDistance ~= nil then
for i=1,#Char do
for l=1,#Surface do
local x[1] = Char[i].LeftLeg:clone()
x[1]:ClearAllChildren()
x[1].name = CharSub[i + l]
x[1].parent = game.Workspace.ImprintableMap
local x[2] = Char[i].RightLeg:clone()
x[2]:ClearAllChildren()
x[2].name = CharSub[i + l + 1]
x[2].parent = game.Workspace.ImprintableMap
local Imprint = Surface[l]:SubtractAsync(x)
Imprint.name = SnowPlane
Imprint.parent = game.Workspace.ImprintableMap
end
end
end
local TickB = elapsedTime()
-- Remove this code --
print(TickB - TickA .. "ms")
break
end
I ended up measuring how far the Char's and Surface's are and if they are too far away not executing that part of the code.
