Huge fps drop when changing Union/MeshParts colors

Hello!,

I am not used to post here but this time i’ve didn’t found any help from somewhere for my problem…

My problem is that i am changing union/MeshParts color3 + Material quickly and i dont know why it cause huge fps drops i dont think the script is the cause of the problem because it does not consume more than 1% (https://gyazo.com/342e4f791687024c00b6e95eca03b61b)

Here is some gif that might help:
https://gyazo.com/3b924ddc1b1589011c51849016e4025d
https://gyazo.com/849c20eb46e6fad948b4386190038d03

I’ve never experienced this before and i hope that i can fix it without changing the current look ^^

I think this should more go to the #help-and-feedback:building-support category. Then this should help you:

I’ve already tried only with parts and Meshpart and it was exactly the same unfortunately.

https://gyazo.com/a11b5946b8104e512489d05ad4e4b9a1

Can you share the code which animates the light bar? As far as I can see the script appears to be the problem to me, not the color/material changes.

1 Like

This is a module script inside my script…

   function TurnOff(Name, LightBar)
    	for _, i in pairs(LightBar:GetChildren()) do
    		if i.Name == Name then
    			i.Material = Enum.Material.SmoothPlastic
    			i.Color = Color3.fromRGB(91, 93, 105)
    		end
    	end
    	for _, i in pairs(LightBar.Lights:GetChildren()) do
    		if i.Name == "Center" then
    			i.Enabled = false
    		end
    	end
    end
    function TurnOn(Name, LightBar)
    	for _, i in pairs(LightBar:GetChildren()) do
    		if i.Name == Name then
    			i.Material = Enum.Material.Neon
    			if Name == "Blue" then
    				i.Color = Color3.fromRGB(13, 105, 172)
    			else
    				i.Color = Color3.fromRGB(255, 0, 0)
    			end
    		end
    	end
    	for _, i in pairs(LightBar.Lights:GetChildren()) do
    		if i.Name == "Center" then
    			if Name == "Blue" then
    				i.Color = Color3.fromRGB(0, 0, 255)
    			else
    				i.Color = Color3.fromRGB(255, 0, 0)
    			end
    			i.Enabled = true
    		end
    	end
    end

    function returnwait(times, CurrentCode, String)
    	if CurrentCode.Value ~= String then
    		return false 
    	else
    		wait(times)
    	end
    end
    local Functions = {
    	Quint = function(LightBar, CurrentCode)
    		local Code = "Quint"
    		local WaitTime = .1
    		while CurrentCode.Value == Code do
    			TurnOn("Blue", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Blue", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOn("Blue", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Blue", LightBar)
    			TurnOn("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOn("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    		end
    	end;
    	
    	Single = function(LightBar, CurrentCode)
    		local Code = "Single"
    		local WaitTime = .2
    		while CurrentCode.Value == Code do
    			TurnOn("Blue", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Blue", LightBar)
    			TurnOn("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Red", LightBar)
    		end
    	end;
    	
    	
    	Cycle = function(LightBar, CurrentCode)
    		local Code = "Cycle"
    		local WaitTime = .1
    		while CurrentCode.Value == Code do
    			TurnOn("Blue", LightBar)
    			TurnOn("Red", LightBar)
    			for _, i in pairs(LightBar.Lights:GetChildren()) do i.Color = Color3.fromRGB(0, 0, 255) end
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Blue", LightBar)
    			TurnOff("Red", LightBar)
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOn("Blue", LightBar)
    			TurnOn("Red", LightBar)
    			for _, i in pairs(LightBar.Lights:GetChildren()) do i.Color = Color3.fromRGB(255, 0, 0) end
    			
    			if returnwait(WaitTime, CurrentCode, Code) == false then break end
    			TurnOff("Blue", LightBar)
    			TurnOff("Red", LightBar)
    			if returnwait(.4, CurrentCode, Code) == false then break end
    		end
    	end;
    	Stop = function(LightBar, CurrentCode)
    		for i = 0,1,.1 do
    			TurnOff("Blue", LightBar)
    			TurnOff("Red", LightBar)
    			wait()
    		end
    	end
    }
    return Functions

Hmm… How many lights are in the Lights part? Additionally, you should add all of your parts & lights to lists. For example, you could have a Parts table with a Blue and Red table which gets created and filled with all of the parts you’re changing when any of your Function items are called (only once).

I’d guess that this would massively improve performance since you’re doing many many GetChildren calls and looping over all children every time you update your parts.

Here’s a tip: Hashmaps are decently fast & easy to work with over table.insert imo. I use hashmaps a LOT. A common example being mapping a “real” object to a “cloned” object.

local someList = {}
for _, part in ipairs(someObject:GetChildren()) do
    if someCondition then
        someList[part] = true
    end
end

while someCondition do
    for part, _ in pairs(someList) do
        doSomeStuff(part)
        if someCondition then
            someList[part] = nil -- Remove it from the list so it gets ignored
        end
    end
end
2 Likes

Okay i tried to put the parts named red into a table named RedTable same for the blue parts and the lights…

Here is the code:

local BlueTable = {}
local RedTable = {}
local LightsTable = {}

for _, i in pairs(script.Parent:WaitForChild("Lightbar").Value:WaitForChild("Lights"):GetChildren()) do
	table.insert(LightsTable, i)
end
for _, i in pairs(script.Parent:WaitForChild("Lightbar").Value:GetChildren()) do
	if i.Name == "Red" then
		table.insert(RedTable, i)
	elseif i.Name == "Blue" then
		table.insert(BlueTable, i)
	end
end

function TurnOff(Name, LightBar)
	local AllTable = {unpack(BlueTable), unpack(RedTable)} 
	for _, i in pairs(AllTable) do
		if i.Name == Name then
			i.Material = Enum.Material.SmoothPlastic
			i.Color = Color3.fromRGB(91, 93, 105)
		end
	end
	for _, i in pairs(LightsTable) do
		i.Enabled = false
	end
end
function TurnOn(Name, LightBar)
	if Name == "Blue" then
		for _, i in pairs(BlueTable) do
			i.Material = Enum.Material.Neon
			i.Color = Color3.fromRGB(13, 105, 172)
		end
	else
		for _, i in pairs(RedTable) do
			i.Color = Color3.fromRGB(255, 0, 0)
			i.Material = Enum.Material.Neon
		end
	end
	for _, i in pairs(LightsTable) do
		if Name == "Blue" then
			i.Color = Color3.fromRGB(0, 0, 255)
		else
			i.Color = Color3.fromRGB(255, 0, 0)
		end
		i.Enabled = true
	end
end

And here is what it does: https://gyazo.com/00a70c7c597816331f7cbb08a6b92de5

It’s a lil bit less laggy but still laggy unfortunately…

I also saw this post: MeshParts cause Engine Lag Is it possible that i am getting the same issue?

https://gyazo.com/39ed132f9418df9bad068ff33c743dc8