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
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
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 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
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.
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
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
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?
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!
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