You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
What I want to achieve is a good way to determine whether a function should be called or not, preferably while avoiding cluttering scripts with too many if statements.
- What is the issue? Include screenshots / videos if possible!
The issue is that my functions interfere with each other. The first triggers an automatic fire procedure, where an object is set on fire, the power goes out, emergency power & alarm & fire extinguishers turn on, which are then disabled and power restored. The issue is that while this function is running, another function that makes a blackout may run. Consequently, the first or the other takes each other out. One function creates a blackout, the other function restores power seconds after. Is there any good way to solve this?
Example:
local function fireAndAutomaticSupression()
print("Start sequence of events")
moduleScript.StartFire()
moduleScript.Blackout()
task.wait(3)
moduleScript.EmergencyPower()
moduleScript.EnableAlarm()
moduleScript.EnableFireExtinguisher()
task.wait(5)
moduleScript.StopFire()
task.wait(1)
moduleScript.DisableFireExtinguisher()
moduleScript.StopAlarm()
moduleScript.RestorePower()
print("Finish sequence of events")
end
This works well, until I add a new function that manipulates the same instances:
local function createBlackoutFire(part)
moduleScript.StartFire()
wait(5)
moduleScript.StopAlarm()
moduleScript.StopAllFireExtinguishers()
moduleScript.Blackout()
end
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I looked a bit on the developer hub, but I did not really find a clear answer. I think I might be able to implement a state machine, where the function checks if it is possible to turn on power, enable / stop fire extinguishers and alarm, et cetera. But then it might look something like:
local function createBlackoutFire(part)
if canStartFire() then
moduleScript.StartFire()
end
wait(5)
if canStopAlarm() then
moduleScript.StopAlarm()
end
if canStopFireExtinguishers() then
moduleScript.StopAllFireExtinguishers()
end
if canBlackout() then
moduleScript.Blackout()
end
end
local function fireAndAutomaticSupression()
print("Start sequence of events")
if canStartFire() then
moduleScript.StartFire()
end
if canBlackout() then
moduleScript.Blackout()
end
task.wait(3)
if canEmergencyPower() then
moduleScript.EmergencyPower()
end
if canEnableAlarm() then
moduleScript.EnableAlarm()
end
if canEnableFireExtinguisher() then
moduleScript.EnableFireExtinguisher()
end
task.wait(5)
if canStopFire() then
moduleScript.StopFire()
end
task.wait(1)
if canDisableFireExtinguisher() then
moduleScript.DisableFireExtinguisher()
end
if canStopAlarm() then
moduleScript.StopAlarm()
end
if canRestorePower() then
moduleScript.RestorePower()
end
print("Finish sequence of events")
end
The latter if statement solution is like taking the lines of code and multiplicating it with 6 or more per function call. This solution therefore seems pretty inefficient and probably easily gets messy and buggy, so are there any other good alternatives? Or would a state machine work if I do it in a better way?
Any suggestions would be very helpful!