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?
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)
so that would āfreezeā all the controls like space, right, left arrows and awsd? and how would i unfreeze it?
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.
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 Controller = require(game.Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")):GetControls()
Controller:Disable() --disables movement
Controller:Enable() --enables movement
I just wanted to update so if anyone stumbled upon this page they would have a working solution
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.
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 ĀÆ\_(ć)_/ĀÆ
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
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)
So that I understand correctly if I am not using the ControlModule should I use the ContextActionService solution from the top of this chat?
The ContextActionService one doesnāt even work when I tested it? @GollyGreg when was the last time you tested that one?
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.
you have to return Enum.ContextActionResult.Sink (is a bug) will update.
I tried that as well and it didnāt seem to work, even though the function was being called
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())
)
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?
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
Extremely strange, Iāll look into seeing if I can reproduce my bug later.
Iām not sure why you necrobumped this post to make that comment and Iām not too sure you read the original post either before doing so. OPās intention is to disable controls while still allowing movement. In your case, youāre only anchoring the root. Controls are still enabled and now the character canāt walk along the designated path.