Hi!
I’m experiencing a problem of which when a seat or a vehicle seat hits the sensor (for the traffic lights), there are major lag spikes on many devices.
-
What do you want to achieve?
Reducing lag/optimizing on a sensor script -
What is the issue?
HUGE lag when a vehicle seat hits the sensor -
What solutions have you tried so far?
Making functions local, using task.wait(), and more.
local function AddSensor(sensor)
sensor.Transparency = 1
local distance
if sensor.Size.X > sensor.Size.Z then distance = sensor.Size.X else distance = sensor.Size.Z end
sensor.Touched:Connect(function(hit)
local b = false
local s = script.Parent.MasterScript.Triggers:FindFirstChild(sensor.Name)
if hit:IsA("Seat") or hit:IsA("VehicleSeat") then
hit.AncestryChanged:Connect(function() b = true end)
repeat s.Value = true task.wait(.1)
until b == true or math.ceil((sensor.Position - hit.Position).magnitude) >= distance
s.Value = false
end
end)
end
local sensors = script.Parent.Sensors:GetChildren()
for i = 1,#sensors do if sensors[i].ClassName == "Part" then AddSensor(sensors[i]) end end