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

Script that tries to make parts not near each other doesn't put them far enough apart?

Asked by 1 year ago
Edited 1 year ago

So I have a script below which is supposed to separate parts when the spawn in to ensure they will not be clumped together, and while it does this partly, for some reason there is a set of parts that have only part of the check complete and they were still placed.

Cords of parts: Part1: (35, 3.5, 32) [] Part2: (31, 3.5, 77) [] As you can see, only the Z-Cords are not within the range I set, while the X-Cords are too close together. I would reccomend checking out line 21 as this is where the check is to see the ranges.

Script in question:

local players = game:GetService("Players")
local replstor = game:GetService("ReplicatedStorage")
local orb = replstor.PointOrb
local redorb = replstor.RedPointOrb
local blueorb = replstor.BluePointOrb
players.PlayerAdded:Connect(function()
    local function Orbspawn(OrbType, MinOrb, MaxOrb, MinRad, MaxRad)
        local lastorbpos
        local orbnum = 1
        local orbamount = math.random(MinOrb,MaxOrb)
        for i = 1, orbamount do
            local orbclone = OrbType:Clone()
            orbclone.Parent = workspace.Orbs
            if orbnum == 1 then
                orbclone.Position = Vector3.new(math.random(MinRad,MaxRad), 3.5, math.random(MinRad,MaxRad))
                lastorbpos = orbclone.Position
                orbnum += 1
            else
                local chosen = false
                while not chosen do
                    local randpos = Vector3.new(math.random(MinRad,MaxRad), 3.5, math.random(MinRad,MaxRad))
                    if randpos.X > lastorbpos.X + 16 or randpos.X < lastorbpos.X - 16 then
                        if randpos.Z > lastorbpos.Z + 16 or randpos.Z < lastorbpos.Z - 16 then
                            orbclone.Position = randpos
                            lastorbpos = randpos
                            orbnum += 1
                            chosen = true
                        else
                            chosen = false
                        end
                    else
                        chosen = false
                    end
                end
            end
        end
    end
    Orbspawn(orb, 5, 10, -200, 200)
    Orbspawn(redorb, 3, 5, -200, 200)
    Orbspawn(blueorb, 1, 2, -200, 200)
end)

Answer this question