Got this really inefficient pressure plate system I built for a tile building system, could someone help me optimize this?
local Open, Close = false, false
while Huma:GetState() ~= Enum.HumanoidStateType.Dead and Tile.Status.Value ~= "Open" do
wait(0.3)
local function PressurePlate(Status, End)
local Color = Color3.new(255,255,255)
local Udim = UDim2.new(1,0,1,0)
if End then
if not Close then
coroutine.wrap(function()
for A = 0,TriggerSpeed*2,0.25 do
if Open == true then Open = false break end
wait(0.25)
end
end
end)()
print("Closing")
Close = true
Udim = UDim2.new(1,0,0,0)
Plate.Progress.SurfaceGui.Frame.BackgroundColor3 = Color
Plate.Progress.SurfaceGui.Frame:TweenSize(Udim,Enum.EasingDirection.Out,Enum.EasingStyle.Quad,TriggerSpeed,true)
end
elseif not Open then
coroutine.wrap(function()
for A = 0,TriggerSpeed*2,0.25 do
if Close == true then Close = false break end
wait(0.25)
end
end
end)()
print("Open")
Open = true
if Status == "Success" then
Color = Color3.new(0,255,0)
elseif Status == "Fail" then
Color = Color3.new(255,0,0)
end
Plate.Progress.SurfaceGui.Frame.BackgroundColor3 = Color
Plate.Progress.SurfaceGui.Frame:TweenSize(Udim,Enum.EasingDirection.Out,Enum.EasingStyle.Quad,TriggerSpeed,true)
end
coroutine.wrap(function()
wait(TriggerSpeed*2)
Close = false
Open = false
end)()
end
if (Plate.Trigger.Position - HRP.Position).magnitude < 3 then
if FindMaterial then -- Requested Material found
if FindMaterial.Value >= DecidedMaterialAmount then
BuyAnimation("Success")
wait(TriggerSpeed)
if (Plate.Trigger.Position - HRP.Position).magnitude < 3 then
FindMaterial.Value -= DecidedMaterialAmount
wait(0.5)
-- Tile update portion
end
else -- Player can't afford
BuyAnimation("Fail")
end
else -- Requested Material not found
BuyAnimation("Fail")
end
else
BuyAnimation(nil, true)
end
end
You could just use one variable for the state of the pressure plate.
You shouldn’t use the global wait() function in big and important structures of the game. You should probably use a timer module.
You should probably create the function outside the scope, because everytime this while loop runs, it would create the same function over and over again, reducing performance.
Tried using a single debounce but for some reason it cause the UI to run without the possibility of it being interrupted (Let’s say I step on the pad and then leave 0.5 seconds after, the UI would still tween open for it’s set amount of time instead of being interrupted and tweening closed when I leave)
Don’t really know much about timer modules, could I get a link to one?
However, if you really wanted optimization I would go with our friend the Touched function. I’m pretty sure that would give you the best performance optimization, with the minor downside that sometimes it doesn’t trigger.
anyways other than that, here’s another cool little trick
We can use the lua’s version of ternary operator (and / or conditions) to turn that into one line
-- thing = (condition) and (if true) or (if false)
Color = Status == "Success" and Color3.new(0,1,0) or Color3.new(1,0,0)
Doesn’t really have any performance benefits, but would definitely help clean up the code a bit!