Lets say you have a sprinting system that lets the player press Shift
to sprint. Then you have an inventory UI that the player can toggle open and close with E
.
When the inventory is open, I want to disable sprinting. So something like:
local inventoryVisible = false
local sprintEnabled = true
local function toggleInventory()
if inventoryVisible then
inventoryVisible = false
sprintEnabled = true
else
inventoryVisible = true
sprintEnabled = false
end
inventoryFrame.Visible = inventoryVisible
end
Now you have a leaderboard UI, which the player can press Tab
to toggle open and close. Opening the leaderboard also disables sprinting. The leaderboard and inventory can both be visible at the same time.
local inventoryVisible = false
local sprintEnabled = true
local leaderboardVisible = false
local function toggleInventory()
if inventoryVisible then
inventoryVisible = false
sprintEnabled = true
else
inventoryVisible = true
sprintEnabled = false
end
inventoryFrame.Visible = inventoryVisible
end
local function toggleLeaderboard()
if leaderboardVisible then
leaderboardVisible = false
sprintEnabled = true
else
leaderboardVisible = true
sprintEnabled = false
end
leaderboardFrame.Visible = leaderboardVisible
end
So now when the player opens the leaderboard or inventory independently, the sprinting is enabled and disabled correctly.
But when the player opens both the leaderboard and inventory and then closes either one, the player can sprint with the leaderboard or inventory open.
The simplest solution is to just check if the other UI is open when toggling. But the flaw is scalability. What if I wanted to add other UI that also disables sprinting? Or what if I wanted to disable everything during a cutscene? I’d have to check if all of them are visible before enabling sprinting.