Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I prevent spawning multiple parts in the same place?

Asked by 8 years ago
local touch = false -- Do not touch/change

function regen(hit)
if not touch then   -- Debounce 


        touch = true                

script.Parent.BrickColor = BrickColor.new("Maroon")
-- Start Of Meat Of Script --
        DD = game.ServerStorage.Dinghy:Clone()

        DD.Parent = game.workspace.RegenDinghy.Spawner
-- End Of the Meat Of Script

wait(3) -- Until usable again

script.Parent.BrickColor = BrickColor.new("Royal purple")   
        print("Good") -- Confirms script went through
        touch = false       

    end

end

script.Parent.Touched:connect(regen)

Here is my perfectly functioning regen script. If it's possible, could I get a heads up on how to prevent multiple parts(in my case, boats) being spawned all at the same spawn in the exact same spot simultaneously? I don't want all the boats joining together.

I was thinking if I made the spawner check if there were parts in its area, but I'm not too sure on how.

Comment if something is confusing.

1 answer

Log in to vote
0
Answered by 8 years ago

Not sure if this will help, but here's a function I wrote which detects if any parts are within another part:

Run = function(BasePart, Bin)
    local Size = BasePart.Size
    local Centre = BasePart.Position
    local XPosMax = Centre.X +Size.X/2
    local XPosMin = Centre.X -Size.X/2
    local YPosMax = Centre.Y +Size.Y/2
    local YPosMin = Centre.Y -Size.Y/2
    local ZPosMax = Centre.Z +Size.Z/2
    local ZPosMin = Centre.Z -Size.Z/2
    local In_Range = {}
    local Scan = nil;
    local Scan = function(Group)
        for _,Parts in pairs(Group:GetChildren()) do
            if Parts.ClassName:match("Part") and (Parts ~= BasePart) then
                local Xs,Ys,Zs = Parts.Size.X,Parts.Size.Y,Parts.Size.Z
                local X,Y,Z,Xl,Yl,Zl = Parts.Position.X+(Xs/2),Parts.Position.Y+(Ys/2),Parts.Position.Z+(Zs/2),Parts.Position.X-(Xs/2),Parts.Position.Y-(Ys/2),Parts.Position.Z-(Zs/2)
                if ((X <= XPosMax) and (Xl >= XPosMin)) and ((Y <= YPosMax) and (Yl >= YPosMin)) and ((Z <= ZPosMax) and (Zl >= ZPosMin)) then
                    table.insert(In_Range, Parts)
                end
            end
            if #Parts:GetChildren() > 0 then
                Scan(Parts)
            end
        end
    end
    Scan(Bin)
    return In_Range
end

So you just run this function with the first parameter being the part which will represent the area. This part can be invisible and cancollided, so it doesn't appear to anyone in-game.

The second parameter is the instance which it will scan. The function will scan ALL descendants of this instance, and return a table of everything which is inside the area part.

Ad

Answer this question