How to stop player movement!


#1

So im trying to add this thing to my tutorial where the game will walk you over to an area using path-finding but how can I stop the player from moving right/left etc?


#2

You can use context action service to do this fairly cleanly.

The following works because ContextActionService works on a priority system. If a higher priority action is bound, it will sink input before filtering down to other actions. It also marks the input action as gameProcessed when checked against some UserInputService functions such as InputBegan.

At the current time, the default control scripts use UserInputService and check if the gameProcessed flag is false before moving your character.

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

ContextActionService:BindActionAtPriority(
    FROZEN_ACTION_KEY,
    function() 
        return Enum.ContextActionResult.Sink
    end,
    false,
    Enum. ContextActionPriority.High.Value
    unpack(Enum.PlayerActions:GetEnumItems())
)

--to unfreeze

ContextActionService:UnbindAction(FROZEN_ACTION_KEY)

#3

so that would “freeze” all the controls like space, right, left arrows and awsd? and how would i unfreeze it?


#4

That would freeze all normal player actions yea. What its doing is replacing the player controls with an empty function. To get all the player actions, we call Enum.PlayerActions:GetEnumItems() which returns an array of all the player actions.

BindActionAtPriority is sorta weird in that it can take an unlimited amount of arguments. Anything at the end of the argument list is added as a new action to listen for.
Unfortunately we cannot pass an array as the final argument of BindActionAtPriority so we call unpack on the list which lists all the elements in the array in order as the final arguments.

You can unfreeze it by calling that last line of code.


#6

OutlookG had a problem with this response as it does not seem to be working anymore,

Here is a different way of doing it

local MasterControl = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("ControlScript"):WaitForChild("MasterControl"))

MasterControl:Disable() --disables movement
MasterControl:Enable() --enables movement

I just wanted to update so if anyone stumbled upon this page they would have a working solution

:slight_smile:


#7

I wouldnt recommend doing this because Control scripts are subject to change their function names at any time. Unless you are forking the ControlScripts while youre doing this, you are subject to code changing without your knowledge and is not future proof.


#8

That is entirely true but I do not think it will be updated anytime soon as the methods are in the
“-- [ Public API ] --” part of the script ¯\_(ツ)_/¯


#9

I am sorry to have to ask but I believe ControlScript no longer appears under PlayerScripts so how is this done now?

OK found it
local ControlModule = require(game.Players.LocalPlayer:WaitForChild(“PlayerScripts”):WaitForChild(“PlayerModule”):WaitForChild(“ControlModule”))

ControlModule:Disable() --disables movement
ControlModule:Enable() --enables movement

:slight_smile:


#10

My method above will work without having to touch or care about the control scripts. It will work in your own project code anywhere you call it.

I’ve already outline the reasons for avoiding using the control scripts in this same thread as well (since the very thing I warned about seemed to happen since this post)


#11

So that I understand correctly if I am not using the ControlModule should I use the ContextActionService solution from the top of this chat?


#12

The ContextActionService one doesn’t even work when I tested it? @GollyGreg when was the last time you tested that one?


#13

I have never tried it. I just tried the last one suggested from the topic and as it didn’t work changed to take into account the latest changes for PlayerScripts.
At the moment that is working so I am OK.
Was just wondering if there was a more favoured one that is not liable to change as was suggested earlier in this topic.


#14

you have to return Enum.ContextActionResult.Sink (is a bug) will update.


#15

I tried that as well and it didn’t seem to work, even though the function was being called :confused:


#16

word for word code I have working on production rn:

ContextActionService:BindActionAtPriority(
	OVERWRITE_CONTROLS_TAG,
	function()
		return Enum.ContextActionResult.Sink
	end,
	false,
	Enum.ContextActionPriority.High.Value + 1,
	unpack(Enum.PlayerActions:GetEnumItems())
)

#17

That’s strange, I have no idea then? The only difference is I used High.Value instead of High.Value + 1, and instead of it always sinking and being contextually added I had it always running like this:

ContextActionService:BindActionAtPriority(
	OVERWRITE_CONTROLS_TAG,
	function()
		return someBool and Enum.ContextActionResult.Sink or Enum.ContextActionResult.Pass
	end,
	false,
	Enum.ContextActionPriority.High.Value,
	unpack(Enum.PlayerActions:GetEnumItems())
)

I found an alternative solution, I’m just confused. Do you fork control scripts?


#18

No, I can send you a studio file where this is working
stopMovement.rbxl (12.7 KB)

There is only a local script in StarterCharacterScripts


#19

Extremely strange, I’ll look into seeing if I can reproduce my bug later.