Hi, I’m coding a splatoon-like game in roblox, where I need to spawn hundreds of paint objects, however, if any two parts are spawned at the same position, I want to overwrite the color of the part instead of spawning a new one there. This is my current code for getting the part at the current position (if it exists)
local partInstanceOverlapParams = OverlapParams.new()
partInstanceOverlapParams.FilterType = Enum.RaycastFilterType.Include
partInstanceOverlapParams:AddToFilter(PaintFolder)
function getPartInstance(object: Part, cframe: CFrame) : Part?
local partsInRadius = workspace:GetPartBoundsInRadius(cframe.Position, 0.1, partInstanceOverlapParams)
for _, part: Part in ipairs(partsInRadius) do
if part.CFrame:FuzzyEq(cframe) then return part end
end
return nil
end
However, this code is unacceptably slow, and causes major lag with the frequency it is called.
Previously, I had a system where it would look up the part in chunk folders given their x and z positions, but I want to be able to have them attach to objects and move, so the chunk folder might not match its new x and y position.
Are there any other properties I could use in a similar look up table, or a more efficient way to do this? Any help would be appreciated.
This just uses the same function that I’m using to detect overlaps, and isn’t what I need. My objects are dynamically created so I need to somehow find whether or not the CFrame I created exists within a current object. The only property I could effectively make a lookup table with is the local position, but the local position on different objects will inevitably overlap a whole lot which will end up being worse cause I’ll have to iterate over a bunch of parts.
use raycast from each part to the nearest parts to check for overlap. Raycasts have been optimized a lot by roblox
Or maybe do math with the size, angle, position for each part to check if they should be overlapping if the parts are rectangles. It might not be efficent
Raycasts are also not nearly fast enough, when I’m doing it on hundreds of parts per frame (About 30,000 per second at its peak)
A decent lookup table would be preferable, but again I’m not sure how I’d make a decent enough lookup table given only the local position