Roblox lagging when setting the transparency of parts (With and without tweening)

So, I am trying to create a lighting system for my vehicles, I tried many methods but all of them turn out to be laggy, but this verison is the most stable one so far, but its still laggy. But in other driving games this doesen’t seem to be an issue. What am i doing wrong?

Video of without tweening and with tweening


image

The handler inside the lights:

LP1 = script.Parent.L1:GetChildren()
LP2 = script.Parent.L2:GetChildren()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local On = TweenInfo.new(0.08, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local Off = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In)

while true do
	wait(0)
	if script.Parent.on.Value == true and script.Parent.led.Value == true then
		task.wait(0.335)
		if script.Parent.on.Value == true then
			for i = 1,#LP1 do
				LP1[i].Braking.Enabled = true
			end

			for i = 1,#LP1 do
				LP1[i].Lights.Enabled = true
			end

			for i = 1,#LP2 do
				LP2[i].Transparency = 0
			end
		end
		task.wait(0.335)

		for i = 1,#LP1 do
			LP1[i].Braking.Enabled = false
		end
				
		for i = 1,#LP1 do
			LP1[i].Lights.Enabled = false
		end

		for i = 1,#LP2 do
			LP2[i].Transparency = 1
		end

	end

	if script.Parent.on.Value == true and script.Parent.led.Value == false then
		task.wait(0.335)
		if script.Parent.on.Value == true then
			for i = 1,#LP1 do
				LP1[i].Braking.Enabled = true
			end

			for i = 1,#LP1 do
				LP1[i].Lights.Enabled = true
			end

			for i = 1,#LP2 do
				TweenService:Create(LP2[i], On, {Transparency = 0}):Play()
			end
		end
		task.wait(0.335)

		for i = 1,#LP1 do
			LP1[i].Braking.Enabled = false
		end

		for i = 1,#LP1 do
			LP1[i].Lights.Enabled = false
		end

		for i = 1,#LP2 do
			TweenService:Create(LP2[i], Off, {Transparency = 1}):Play()
		end
	end
end

This is because you are using a deprecated method when setting the transparency of the basepart. Instead you should do, “BasePart:SetTransparency.”

it gives me this error “SetTransparency is not a valid member of Part”
Additionally, does this functionality only apply to parts? Because i also use MeshParts in some vehicles

a BasePart refers to Unions, Terrain, Parts, MeshParts, and everything that can be seen with the properties from BasePart. SetTransparency is not a valid member of BasePart.

I checked the documentation of BasePart and there is no method :SetTransparency, where is this exactly?

1 Like

I think it might the problem that you create the Tween and play it every frame? Check your tween to see, play tween everyframe overlapping with others might caused the problem

Well man,. you have more then 3 for loops running inside of a while loop that is running super fast

Also looping through something with ipairs is faster then using a for loop, im sure you can achieve the same things if you switch to a pairs loop

I tried this but i get the same results.

local LP1 = script.Parent.L1:GetChildren()
local LP2 = script.Parent.L2:GetChildren()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local On = TweenInfo.new(0.08, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local Off = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In)

local function enableLights()
	for _, light in ipairs(LP1) do
		light.Braking.Enabled = true
		light.Lights.Enabled = true
	end
	for _, part in ipairs(LP2) do
		part.Transparency = 0
	end
end

local function disableLights()
	for _, light in ipairs(LP1) do
		light.Braking.Enabled = false
		light.Lights.Enabled = false
	end
	for _, part in ipairs(LP2) do
		part.Transparency = 1
	end
end

local function tweenEnableLights()
	for _, light in ipairs(LP1) do
		light.Braking.Enabled = true
		light.Lights.Enabled = true
	end
	for _, part in ipairs(LP2) do
		TweenService:Create(part, On, {Transparency = 0}):Play()
	end
end

local function tweenDisableLights()
	for _, light in ipairs(LP1) do
		light.Braking.Enabled = false
		light.Lights.Enabled = false
	end
	for _, part in ipairs(LP2) do
		TweenService:Create(part, Off, {Transparency = 1}):Play()
	end
end

while true do
	wait(0)
	if script.Parent.on.Value == true then
		task.wait(0.335)
		if script.Parent.on.Value == true then
			if script.Parent.led.Value == true then
				enableLights()
			else
				tweenEnableLights()
			end
			task.wait(0.335)
			if script.Parent.led.Value == true then
				disableLights()
			else
				tweenDisableLights()
			end
		end
	end
end

Yeah, you’re still looping a lot of times, lets ask some questions

Do we need the while loop?
Do we need to loop through each part and tween a transparency?
Do we need to loop through light and enable braking and lights?

Also when you are using a for loop don’t use ipairs or pairs they’re deprecated now and dont really have much of a use.

for _, light in (LP1) do

end

Let’s try to remove all these loops though, like for instance the while loop is not needed since you are just checking if a value turns on.

script.Parent.on.Changed:Connect(function()
     if script.Parent.on.Value == true then

     end
end)
-- or
script.Parent.On:GetPropertyChangedSignal("Value"):Connect(function()
     if script.Parent.on.Value == true then

     end
end

Do we need to loop through each part and tween a transparency? Is the tween really needed? or can you just set it to 0 and 1 without tweening? Try using unions, union all the parts that you’d want to change the transparency of and now you only need to change one union part. You can also do this for the lights if needed.

I’d also like to know what the light.Braking.Enabled = false and the light.Lights.Enabled = false is meant for. Try to think of ways where you dont have to loop especially if isn’t visual, if you are enabling and disabling scripts then that is probably what is causing the lag.

Try this, I have made some optimizations.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Model = script.Parent

local LP1 = Model.L1:GetChildren()
local LP2 = Model.L2:GetChildren()

local OnTweenInfo = TweenInfo.new(0.08, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local OffTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In)

local function LightsToggle(Bool)
	for _, v in pairs(LP1) do
		v.Braking.Enabled = Bool
		v.Lights.Enabled = Bool
	end
	
	for _, v in pairs(LP2) do
		v.Transparency = Bool and 0 or 1
	end
end

local function TweenLights(Bool)
	for _, v in pairs(LP1) do
		v.Braking.Enabled = Bool
		v.Lights.Enabled = Bool
	end
	
	for _, v in pairs(LP2) do
		TweenService:Create(v, Bool and OnTweenInfo or OffTweenInfo, {Transparency = Bool and 0 or 1}):Play()
	end
end

Model.on.Changed:Connect(function(Value)
	if not Value then return end
	
	if Model.led.Value then
		LightsToggle(Value)
		task.wait(0.335)
		LightsToggle(not Value)
	else
		TweenLights(Value)
		task.wait(0.335)
		TweenLights(not Value)
	end
end)

Do we need the while loop?
I think because the light needs to turn on and off because im usning this as an indicator but maybe i could do this like turning the on value on and off with the main script but im not sure how to do that

this is the script that controls the values
image

light.Braking.Enabled = false and light.Lights.Enabled = false they refer to these in L1
the braking is an image if i wanna use a image instead of a part
image

Do we need to loop through each part and tween a transparency? Is the tween really needed? or can you just set it to 0 and 1 without tweening?
i am trying to make like the halogen lights for old cars and i think that requires tweens

Do we need to loop through light and enable braking and lights?
I need the lights but braking is not necessary

The on value stays on the entire time so the light wont like blink but i could solve this by modifying the main on handler but im not sure how to do that. any idea on how to make that work?
image

made it like this but now i cant even stop the loop and when tweening it still lags

I was just about to upload a copy with only the lights, no body of the car and its not laggy now. Any idea what may cause this?

Without looking at the code it’s hard to say … the use of heartbeat in other parts of your program or super fast loops can cause problems elsewhere in the program. My guess is you’re using heartbeat someplace … IDK, I do know that material switch is very clean. It has become my go to method for things like this. Good luck!

i haven’t deleted any scripts just the body and it stopped lagging but heres a copy without the body and with deleted scripts.
Lights2.rbxm (211.9 KB)

so now i tried deleting all the other scripts from the original model which didn’t seem to help but i put the body and the lights into another chassis and it did not lag there

I might have found the problem, there was a humanoid “shadow” in my car, removed in and now it does not lag but the tweening is not working right.

image

EDIT: Yeah this was it, published the game and the tween is working fine outside of studio and not lagging anymore

That looks great smooth as glass … Good luck!