Freeze a player when he: opens/writes in chat, presses escape to menu, or switches the roblox tab to others

Wassup guys. This is my second time creating a topic in roblox!

Anyways, I need help: I want to make a script that if a player opens chat, or presses escape to menu, or switches other tabs from roblox, he will freeze, But if closes chat, closes the menu, or switches back to the roblox tab, he will be unfreezed. It’s like an afk, but he instead freezes. Can you guys help me?

Best, XMLcard

You can use UIS and GuiService for that.

local UIS = game:GetService("UserInputService")
local GuiS = game:GetService("GuiService")
local plrs = game:GetService("Players")

local plr = plrs.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")

local defaultWalkSpeed, defaultJumPower = hum.WalkSpeed, hum.JumpPower
local lastWalkSpeed, lastJumpPower
local isFreeze = false

local function freeze()    
    if isFreeze then return end
    isFreeze = true

    lastWalkSpeed = hum.WalkSpeed
    lastJumpPower = hum.JumpPower

    hum.WalkSpeed = 0
    hum.JumpPower = 0
end

local function unfreeze()
    if not isFreeze then return end
    isFreeze = false

    hum.WalkSpeed = lastWalkSpeed or defaultWalkSpeed
    hum.JumpPower = lastJumpPower or defaultJumpPower
end

UIS.TextBoxFocused:Connect(freeze)
UIS.WindowFocused:Connect(freeze)
GuiS.MenuOpened:Connect(freeze)

UIS.TextBoxFocusReleased:Connect(unfreeze)
UIS.WindowFocusReleased:Connect(unfreeze)
GuiS.MenuClosed:Connect(unfreeze)
2 Likes

Yo man you are the goats but the output’s answer: WindowFocus is not a valid member of RunService “Run Service”.

For both .WindowFocus and .WindowFocusReleased, I think he means

--  https://create.roblox.com/docs/reference/engine/classes/UserInputService#WindowFocused
UIS.WindowFocused:Connect(freeze)

and

-- https://create.roblox.com/docs/reference/engine/classes/UserInputService#WindowFocusReleased
UIS.WindowFocusReleased:Connect(unfreeze)

respectively?

1 Like

Another goat! Thanks, I will try it now to see if it works

1 Like

Yes, I’m sorry for that. I was on cellphone and forgot to type correctly. Thanks for correcting me!

Did it work?

This text will be blurred