Disabling Controls issue

Hey there I am having a small issue, the goal is the disable the users controls while their character attribute called IsCombat is true, but the moment it becomes false the user needs to press on “w” or “s” again to be able to move. It’s ruining the flow of the combat. Any idea how to fix that?

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

local playerScripts = localPlayer:WaitForChild("PlayerScripts")
local PlayerModule = require(playerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()

character:GetAttributeChangedSignal("AirCombat"):Connect(function()
    if character:GetAttribute("AirCombat") then
        Controls:Disable()
    else
        task.wait(0.1)
        Controls:Enable()
    end
end)

localPlayer.CharacterAdded:Connect(function(newCharacter)
    character = newCharacter
end)

There is an alternative to completely disable controls via ContextActionService.

Here are snippets of the code that can get you interested.

local ContextActionService = game:GetService("ContextActionService")
local FREEZE_ACTION = "freezeMovement"

ContextActionService:BindAction(
    FREEZE_ACTION,
    function() return Enum.ContextActionResult.Sink end,
    false,
    unpack(Enum.PlayerActions:GetEnumItems())
)

--to unfreeze

ContextActionService:UnbindAction(FREEZE_ACTION)
1 Like

I had a lot of problems when I was toggling the player’s controls. It seems unreliable.

I decided to just anchor the HumanoidRootPart instead.

Maybe that would work for you.

Is there a specific reason? What were problems? I need an elaborate idea.

I just remember that the Controls would frequently NOT return when requested. So it caused me a lot of problems.