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

How To Fix Lagging The More You Fire A Function Or An Event?

Asked by 4 years ago
Edited 4 years ago

--[ Details ]-- Hello, I am trying to make a neon-like party and there has been a bug, right now im using module script because i want the other script to use the function as well

There is a Brick That Have ClickDetector , and A Script

There is also a folder that contains NeonParts, Each Part is named a Number

--[ Problems ]--

  • When I Click The Brick, The Function from module script activate, But The More I Click, I Noticed It starting to get laggy, I also tried putting "Print()" inside the module script, and they did print it and not multiple times, The problem is just it gets laggy the more i click

  • I also tried this in-game and also tried the micro-profile , And it is confirmed that it lag since The more i click the button, The micro profile just goes up and the tween just kinda lag

--[ The Script ]--

This is the BrickButton Script

local ClickDetector = script.Parent.ClickDetector
local NeonParts = game.Workspace.NeonTestGroup
local Module = require(game.ReplicatedStorage.LightningModuleScript)
local ChosenColor = script.Parent.Parent.ChosenColor
local Debounce = script.Parent.Parent.Debounce

ClickDetector.MouseClick:Connect(function()
    wait()
    local Neonpartschildren = NeonParts:GetChildren()
    local Childrenscount = #Neonpartschildren

    if Debounce.Value == false then
        Debounce.Value = true

        local RVal = ChosenColor.Value.R * 255
        local GVal = ChosenColor.Value.G * 255
        local BVal = ChosenColor.Value.B * 255

        for i,v in pairs(NeonParts:GetChildren()) do
            if v:IsA("Folder") then
                wait()

                for ii,c in pairs(v:GetChildren()) do
                    Module.ColorChange(c,RVal,GVal,BVal)
                end

                if i == Childrenscount then
                    Debounce.Value = false
                end
            end
        end
    end
end)

And This is The ModuleScript

local module = {}

module.ColorChange = function(NeonPart,R,G,B) -- This is for color changing lol

    local Tween = game:GetService("TweenService")
    local Info = TweenInfo.new(0.3,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
    local Goal = {
        Color = Color3.fromRGB(91,93,105)
    }

    active = false

    NeonPart:GetPropertyChangedSignal("BrickColor"):Connect(function()
        local NeonTween = Tween:Create(NeonPart,Info,Goal)
        if NeonPart.Color == Color3.fromRGB(R,G,B) and active == false then
            NeonTween:Play()
            active = true
            NeonTween.Completed:Connect(function()
                active = false
            end)
        end
    end)

    NeonPart.Color = Color3.fromRGB(R,G,B)
    print("OOOF?")
end


return module

Please Help As I Been Stuck With This Since Yesterday, Thanks in Advanced!

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

You problem is that everytime you click, you are making a new connection, without disconnecting the old one. Roblox allows to have multiple connections to the same event, so they just stacking up, causing a lag.

The solution will depend on what you are trying to achieve, but I will make a educated guess that after the tween starts, you no longer need the connection.

    local neonPartConnection = NeonPart:GetPropertyChangedSignal("BrickColor"):Connect(function()
        local NeonTween = Tween:Create(NeonPart,Info,Goal)
        if NeonPart.Color == Color3.fromRGB(R,G,B) and active == false then
            NeonTween:Play()
        neonPartConnection:Disconnect()
            active = true
            NeonTween.Completed:Connect(function()
                active = false
            end)
        end
    end)

Hope this helps.

Edit: I moved the disconnect, to make sure function will disconnect if different tween interrupts the old one.

0
DUDE THANKS IT WORKS, Thanks so much! Minecrafter09031031 16 — 4y
Ad

Answer this question