#Performance issue

31 messages · Page 1 of 1 (latest)

rotund oasis
#
function leafOnEarningArea()
    for _, leaf in ipairs(leafFolder:GetChildren()) do
        if leaf:IsA("BasePart") and leaf.Name == "Leaf" then
            leaf.Touched:Connect(function(hit)
                if hit == earningArea then
                    if debounce == false then
                        debounce = true
                        leafSuprresion.Value = leafSuprresion.Value - 1
                        leaf:Destroy()
                        getStat()
                        debounce = false
                    end
                end
            end)
        end
    end
end

function checkColisionWithBlower()
    for _, leaf in ipairs(leafFolder:GetChildren()) do
        if leaf:IsA("BasePart") and leaf.Name == "Leaf" then
            leaf.Touched:Connect(function(hit)
                if hit == blowerHitBox then
                    if debounce == false then
                        debounce = true 
                        local direction = (leaf.Position - blowerHitBox.Position).Unit
                        leaf.AssemblyLinearVelocity = direction * 30 -- Expulse la feuille
                        task.wait(0)
                        debounce = false
                    end
                end
            end)
        end
    end
end

-- Recuper les stat du joueur au nécesaire et de les mettres à jours
function getStat()
    local player = Players.LocalPlayer
    local leaderstats = player:WaitForChild("leaderstats")
    local stat = leaderstats:WaitForChild("Leaf")
    task.wait(0)
    stat.Value += 1
    return
end

while true do
    checkColisionWithBlower()
    leafOnEarningArea()
    task.wait(0) -- Boucle rapide
end
#

I get some performanse issue with this code, any idea ?

glacial rapids
#

Use Collection service bro.

frigid wind
#

my idea is to learn how to code, every while loop you're connecting 2 .Touched event

frigid wind
glacial rapids
frigid wind
#

there is some use cases but almost always is gonna be a better sol

#

it works = its not the best

glacial rapids
#

Then what's the better optino

frigid wind
#

and if you're going to make a project or game with the first thing that works lol

frigid wind
#

depends on how is his game organised

terse musk
#

is it possible to switch this around so that the blower is the one checking collision with leaves? (less touched connections)

frigid wind
#

exactly, every loop in the while hes connecting TWO connections of touched event, in js 1 second there can be already 40 connections

terse musk
rotund oasis
#

This is reasons

#

@terse musk and @frigid wind

#

And the spawn of a leaf is organise this way :

#

a

#
local leafSpawnArea = workspace.Area:WaitForChild("leafSpawnArea")  
local maxLeaf = 30  
local actualLeaf = Instance.new("IntValue") 
actualLeaf.Name = "actualLeaf"  
actualLeaf.Parent = game.Workspace:WaitForChild("leafFolderPart")  
actualLeaf.Value = 0 

function triggerRandomEvent()
    local minX, maxX, minZ, maxZ = limiteOfSpawnArea()
    local randomIndex = math.random(1, 3) 

    if randomIndex == 1 then  
        if actualLeaf.Value < maxLeaf then  -
            local leaf = Instance.new("Part") 
            leaf.Size = Vector3.new(1, 0.5, 1) 
            leaf.BrickColor = BrickColor.new("Tr. Green") 
            leaf.Anchored = false  
            leaf.Name = "Leaf"  
            leaf.CanCollide = true 
            local randomX = math.random(minX, maxX)  
            local randomZ = math.random(minZ, maxZ) 
            local yPosition = leafSpawnArea.Position.Y + 10  

            leaf.Position = Vector3.new(randomX, yPosition, randomZ)
            leaf.Parent = workspace:WaitForChild("leafFolderPart") 

            actualLeaf.Value = actualLeaf.Value + 1 
        end    
    end
end


function limiteOfSpawnArea()
    local leafSpawnAreaPosition = leafSpawnArea.Position  
    local leafSpawnAreaSize = leafSpawnArea.Size  

    local minX = (leafSpawnAreaPosition.X - leafSpawnAreaSize.X / 2) + 3  
    local maxX = (leafSpawnAreaPosition.X + leafSpawnAreaSize.X / 2) - 3  
    local minZ = (leafSpawnAreaPosition.Z - leafSpawnAreaSize.Z / 2) + 3 
    local maxZ = (leafSpawnAreaPosition.Z + leafSpawnAreaSize.Z / 2) - 3  

    return minX, maxX, minZ, maxZ
end


while true do 
    triggerRandomEvent()  
    wait(1)
end
frigid wind
#

when i finish a call ill explain how if someone didnt already

#

basically when u create a new leafe connect to it a .Touched event, just one for it

#

leaf.Touched

#

and there do the logic of givin the player the things and all that

rotund oasis
#

ok

#

When i debug, i think i have to much connection with .touched

loud plumeBOT
#

studio** You are now Level 3! **studio

rotund oasis
#

I will try to use an other event

terse musk
#

try having the leaves be anchored until they are interacted with to see if that might help the performance