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

How to prevent server lag with GetPartsInPart?

Asked by 2 years ago

How do I Filter so InsideResults Only works with parts name Engine, Handle or HumanoidRootPart to prevent lag.

local ToucherPart = script.Parent["Meshes/Sphere"]

local SG = script:WaitForChild("ShipGravity")
local ID = script:WaitForChild("GravityID")

--Shuttle Addition
if (_G.all_Gateships == nil) then
    _G["all_Gateships"] = {}
end
ALL = _G.all_Gateships
shared["all_Gateships"] = _G.all_Gateships

--Models Addition
if (_G.all_Models == nil) then
    _G["all_Models"] = {}
end
ALLMODELS = _G.all_Models
shared["all_Models"] = _G.all_Models



function CloneAdd(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemove(player)
    local Gravity = player.Character:FindFirstChild(SG.Name)
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end



function CloneAddShuttle(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    local ShuttleEngine = part.Parent:FindFirstChild("Engine")
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemoveShuttle(ShuttleModel)
    local Gravity = ShuttleModel:FindFirstChild(SG.Name)
    local ShuttleEngine = ShuttleModel:FindFirstChild("Engine")
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end



function CloneAddModel(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    local ModelHandle = part.Parent:FindFirstChild("Handle")
    if not ModelHandle then return end
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemoveModel(ModelForGrav)
    local Gravity = ModelForGrav:FindFirstChild(SG.Name)
    local ModelHandle = ModelForGrav:FindFirstChild("Handle")
    if not ModelHandle then return end
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end



while task.wait(0.5) do 
    --print ("Touching Zone Printing")

    local InsideResults = workspace:GetPartsInPart(ToucherPart)
    --local OutsideResults = workspace:GetPartsInPart(ToucherPart)

    --print (InsideResults)
    --print (OutsideResults)

    local modeltable = {}


    for _, part in pairs(InsideResults) do
        local humanoid = part.Parent:FindFirstChild("Humanoid")
        if humanoid and not modeltable[humanoid.Parent] then

            modeltable[part.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAdd(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        local ShuttleEngine = part.Parent:FindFirstChild("Engine")
        if ShuttleEngine and not modeltable[ShuttleEngine.Parent] then

            modeltable[ShuttleEngine.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAddShuttle(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        local ModelHandle = part.Parent:FindFirstChild("Handle")
        if ModelHandle and not modeltable[ModelHandle.Parent] then

            modeltable[ModelHandle.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAddModel(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        --print (modeltable)

    end


    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do

        local character = player.Character
        if character and not modeltable[character] then --if character and not modeltable[character] then
            local humanoid = character:FindFirstChild("Humanoid")
            if humanoid then

                local Gravity = player.Character:FindFirstChild(SG.Name)

                if Gravity then
                    CloneRemove(player)
                end
                --print (player.Character.Name.." Is Outside Zone")
            end
        end
    end

    for i = 1, #ALL do

        local ShuttleModel = ALL[i].Model
        if ShuttleModel and not modeltable[ShuttleModel] then --if character and not modeltable[character] then
            local Gravity = ShuttleModel:FindFirstChild(SG.Name)
            if Gravity then
                CloneRemoveShuttle(ShuttleModel)
            end
            --print (ShuttleModel.Name.." Is Outside Zone")
        end
    end

    for i = 1, #ALLMODELS do

        local ModelForGrav = ALLMODELS[i]
        if ModelForGrav and not modeltable[ModelForGrav] then --if character and not modeltable[character] then
            local Gravity = ModelForGrav:FindFirstChild(SG.Name)
            if Gravity then
                CloneRemoveModel(ModelForGrav)
            end
            --print (ModelForGrav.Name.." Is Outside Zone")
        end
    end




    --  Clearing Table
    ------------------- 
    modeltable = {}
    -------------------
end

2 answers

Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

Use :GetPartBoundsInBox() and use overlaps params(basically RaycastParams but for GetPartBoundsInBox)

local ToucherPart = script.Parent["Meshes/Sphere"]

local SG = script:WaitForChild("ShipGravity")
local ID = script:WaitForChild("GravityID")
local IgnoreTable = {}
--Shuttle Addition
if (_G.all_Gateships == nil) then
    _G["all_Gateships"] = {}
end
ALL = _G.all_Gateships
shared["all_Gateships"] = _G.all_Gateships

--Models Addition
if (_G.all_Models == nil) then
    _G["all_Models"] = {}
end
ALLMODELS = _G.all_Models
shared["all_Models"] = _G.all_Models



function CloneAdd(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemove(player)
    local Gravity = player.Character:FindFirstChild(SG.Name)
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end



function CloneAddShuttle(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    local ShuttleEngine = part.Parent:FindFirstChild("Engine")
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemoveShuttle(ShuttleModel)
    local Gravity = ShuttleModel:FindFirstChild(SG.Name)
    local ShuttleEngine = ShuttleModel:FindFirstChild("Engine")
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end



function CloneAddModel(part)
    local Gravity = part.Parent:FindFirstChild(SG.Name)
    local ModelHandle = part.Parent:FindFirstChild("Handle")
    if not ModelHandle then return end
    if not Gravity then
        SG:Clone().Parent = part.Parent
    end
end
function CloneRemoveModel(ModelForGrav)
    local Gravity = ModelForGrav:FindFirstChild(SG.Name)
    local ModelHandle = ModelForGrav:FindFirstChild("Handle")
    if not ModelHandle then return end
    if Gravity then
        if Gravity.Value == SG.Value then
            Gravity:Destroy()
        end
    end
end

for Index,Value in pairs(Table) do
    if Value.Name == "Handle" or Value.Name == "Engine" or Value.Name == "HumanoidRootPart" then 
    table.insert(IgnoreTable,Value) 
    end 
end
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Whitelist
Params.FilterDescendantsInstances = IgnoreTable
while task.wait(0.5) do 
    --print ("Touching Zone Printing")

    local InsideResults = workspace:GetPartBoundsInBox(ToucherPart.CFrame,ToucherPart.Size,Params)
    --local OutsideResults = workspace:GetPartsInPart(ToucherPart)

    --print (InsideResults)
    --print (OutsideResults)

    local modeltable = {}


    for _, part in pairs(InsideResults) do
        local humanoid = part.Parent:FindFirstChild("Humanoid")
        if humanoid and not modeltable[humanoid.Parent] then

            modeltable[part.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAdd(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        local ShuttleEngine = part.Parent:FindFirstChild("Engine")
        if ShuttleEngine and not modeltable[ShuttleEngine.Parent] then

            modeltable[ShuttleEngine.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAddShuttle(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        local ModelHandle = part.Parent:FindFirstChild("Handle")
        if ModelHandle and not modeltable[ModelHandle.Parent] then

            modeltable[ModelHandle.Parent] = true

            local Gravity = part.Parent:FindFirstChild(SG.Name)
            if not Gravity then
                CloneAddModel(part)
            end
            --print (part.Parent.Name.." Is Inside Zone")
        end

        --print (modeltable)

    end


    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do

        local character = player.Character
        if character and not modeltable[character] then --if character and not modeltable[character] then
            local humanoid = character:FindFirstChild("Humanoid")
            if humanoid then

                local Gravity = player.Character:FindFirstChild(SG.Name)

                if Gravity then
                    CloneRemove(player)
                end
                --print (player.Character.Name.." Is Outside Zone")
            end
        end
    end

    for i = 1, #ALL do

        local ShuttleModel = ALL[i].Model
        if ShuttleModel and not modeltable[ShuttleModel] then --if character and not modeltable[character] then
            local Gravity = ShuttleModel:FindFirstChild(SG.Name)
            if Gravity then
                CloneRemoveShuttle(ShuttleModel)
            end
            --print (ShuttleModel.Name.." Is Outside Zone")
        end
    end

    for i = 1, #ALLMODELS do

        local ModelForGrav = ALLMODELS[i]
        if ModelForGrav and not modeltable[ModelForGrav] then --if character and not modeltable[character] then
            local Gravity = ModelForGrav:FindFirstChild(SG.Name)
            if Gravity then
                CloneRemoveModel(ModelForGrav)
            end
            --print (ModelForGrav.Name.." Is Outside Zone")
        end
    end




    --  Clearing Table
    ------------------- 
    modeltable = {}
    -------------------
end
0
How do I add Engine, HumanoidRootPart and handles to the Filter Descendants Array, that's the part I am stuck on BearKranz 41 — 2y
0
? Puppynniko 1059 — 2y
0
I know i need to use this: FilterDescendantsInstances . But i dont get how to scan the workspace and add to that table the named parts i need. BearKranz 41 — 2y
View all comments (6 more)
0
what are you trying to do? Puppynniko 1059 — 2y
0
In order to use FilterDescendantsInstances it needs a table to ignore Parts etc, I need a way to loop through the workspace and find All Parts with the name Handle, Engine or HumanoidRootPart, and insert them to that table. BearKranz 41 — 2y
0
first use the Whitelist FilterType then loop through a folder or the path where those files where located if you dont know loop through workspace with getdescendants but i dont recommend this bc this causes alot of lag now that you got the table of it you can do Puppynniko 1059 — 2y
0
for Index,Value in pairs(Table) do if Value.Name == "Handle" or Value.Name == "Engine" or Value.Name == "HumanoidRootPart" then table.insert(IgnoreTable,Value) end end Puppynniko 1059 — 2y
0
i've edited the script with the Getinbounds setup Puppynniko 1059 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Workspace.ShipGravZone.ZoneGravity:270: invalid argument #1 to 'pairs' (table expected, got nil)

0
that table is supposed to be workspace.TheParent of Where Handle Engine And HumanoidRootPart:GetChildren() is supposed to be Puppynniko 1059 — 2y
0
since idk your explorer i cant provide it Puppynniko 1059 — 2y
0
They are inside Models inside workspace. BearKranz 41 — 2y
0
then workspace.Models Puppynniko 1059 — 2y
0
Not a Model called Models, Iniside the ClassCalled Model BearKranz 41 — 2y

Answer this question