Here’s a pretty simple snippet:
game:GetService("UserInputService").InputEnded:connect(function(ib)
if ib.UserInputType == Enum.UserInputType.Focus then
print("Roblox client has lost focus")
end
end)
game:GetService("UserInputService").InputBegan:connect(function(ib)
if ib.UserInputType == Enum.UserInputType.Focus then
print("Roblox client has regained focus")
end
end)
game:GetService("UserInputService").InputChanged:connect(function(ib)
if ib.UserInputType == Enum.UserInputType.Focus then
print("Roblox client has changed focus")
end
end)
The problem is, it prints nothing in online mode. It does work in studio.
Why would you need to know if the window is in focus or not? Here are some examples:
- Run things at lower frequency when focus is lost to conserve power (much like roblox studio).
- Stop actions when focus is lost. (clicking on the taskbar to switch tasks, pressing Alt and Tab, gamepad input)
- Automatically pause the game when focus is lost.
This place is a repro of the bug.