One way I can think of doing it would be to have the WalkSpeed set to 0, but when you are pressing down ‘S’ (or the other device equivalents) it sets your WalkSpeed to 16 or whatever you want it to be.
I spent about two minutes quickly testing my idea, here it is if you want it (just put it in a localscript in StarterCharacter or StarterGui)
local Player = game.Players.LocalPlayer
local InputKey = "S"
repeat wait() until Player.Character
local Humanoid = Player.Character:WaitForChild("Humanoid")
Humanoid.WalkSpeed = 0
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:connect(function(UserInput, GameProcessedEvent)
if UserInput.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent then
if UserInput.KeyCode == Enum.KeyCode[InputKey] then
Humanoid.WalkSpeed = 16
elseif UserInput.KeyCode == Enum.KeyCode["W"] then
Humanoid.WalkSpeed = 0
end
end
end
end)
UserInputService.InputEnded:connect(function(UserInputEnded, GameProcessedEvent)
if UserInputEnded.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent and UserInputEnded.KeyCode == Enum.KeyCode[InputKey] then
Humanoid.WalkSpeed = 0
end
end
end)
This does check out, although there is one fatal flaw.
Users can manipulate this by pressing A and D while walking backwards, although this is obviously a given considering you didn’t code with this in mind. (I’m also not sure if this flaw is actually preventable?)
Not too sure on a fix.
I’m open to any and all ideas.
You can easily do the same with what I did for when you press W, that you can’t press A or D either, I spent another minute configuring my code for ya.
local Player = game.Players.LocalPlayer
local InputKey = "S"
repeat wait() until Player.Character
local Humanoid = Player.Character:WaitForChild("Humanoid")
Humanoid.WalkSpeed = 0
local UserInputService = game:GetService("UserInputService")
local movingSideways = false
UserInputService.InputBegan:connect(function(UserInput, GameProcessedEvent)
if UserInput.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent then
if UserInput.KeyCode == Enum.KeyCode[InputKey] and not movingSideways then
Humanoid.WalkSpeed = 16
elseif UserInput.KeyCode == Enum.KeyCode["W"] then
Humanoid.WalkSpeed = 0
elseif UserInput.KeyCode == Enum.KeyCode["A"] then
movingSideways = true
Humanoid.WalkSpeed = 0
elseif UserInput.KeyCode == Enum.KeyCode["D"] then
movingSideways = true
Humanoid.WalkSpeed = 0
end
end
end
end)
UserInputService.InputEnded:connect(function(UserInputEnded, GameProcessedEvent)
if UserInputEnded.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent then
if UserInputEnded.KeyCode == Enum.KeyCode[InputKey] then
Humanoid.WalkSpeed = 0
elseif UserInputEnded.KeyCode == Enum.KeyCode["A"] then
movingSideways = false
elseif UserInputEnded.KeyCode == Enum.KeyCode["D"] then
movingSideways = false
end
end
end
end)
I had to fix a little glitch so this is the final code, let me know if you want me to explain anything (:
local Player = game.Players.LocalPlayer
local InputKey = "S"
repeat wait() until Player.Character
local Humanoid = Player.Character:WaitForChild("Humanoid")
Humanoid.WalkSpeed = 0
local UserInputService = game:GetService("UserInputService")
local movingSidewaysA, movingSidewaysD = false, false
UserInputService.InputBegan:connect(function(UserInput, GameProcessedEvent)
if UserInput.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent then
if UserInput.KeyCode == Enum.KeyCode[InputKey] and not movingSidewaysA and not movingSidewaysD then
Humanoid.WalkSpeed = 16
elseif UserInput.KeyCode == Enum.KeyCode["W"] then
Humanoid.WalkSpeed = 0
elseif UserInput.KeyCode == Enum.KeyCode["A"] then
movingSidewaysA = true
Humanoid.WalkSpeed = 0
elseif UserInput.KeyCode == Enum.KeyCode["D"] then
movingSidewaysD = true
Humanoid.WalkSpeed = 0
end
end
end
end)
UserInputService.InputEnded:connect(function(UserInputEnded, GameProcessedEvent)
if UserInputEnded.UserInputType == Enum.UserInputType.Keyboard then
if not GameProcessedEvent then
if UserInputEnded.KeyCode == Enum.KeyCode[InputKey] then
Humanoid.WalkSpeed = 0
elseif UserInputEnded.KeyCode == Enum.KeyCode["A"] then
movingSidewaysA = false
elseif UserInputEnded.KeyCode == Enum.KeyCode["D"] then
movingSidewaysD = false
end
end
end
end)
You need it because otherwise you can hold down A, then D, release D which would cause the code to count it that you aren’t moving sideways, so then you would be holding A still and then when you press W you would be able to move diagonally. It is very much needed to prevent that bug.
You can do this in far less lines and in a supported manner. Instead of modifying the character’s walk speed, go after the actual control bindings themselves.
local ContextActionService = game:GetService("ContextActionService")
local REMOVE_BINDS = {"moveForwardAction", "moveLeftAction", "moveRightAction"}
for _, actionBinding in ipairs(REMOVE_BINDS) do
ContextActionService:UnbindAction(actionBinding)
end
I’ve actually started development on the short game I’m making based on this.
I’m marking this now as the solution as it is shorter and generally more efficient - users can spam all their movement keys while walking backward and not suddenly stop if they were walking backward and pressed A or D or W, which was a flaw of the other script.
That code works with the expectation that you won’t rebind the actions during the game again, because there isn’t a way to fetch the relevant functions other than through the PlayerModule itself.
What I need to do is just disable movement sideways and backwards for a bit then re enable, how would I do that in lets say a local script which I fire a remote to from the server, what im doing is firing that remote when i want the effects to take place and then again when i want the movement to return to normal. so how would i do all of that in a local script
Bind a sink for CharacterLeft, CharacterRight and CharacterBackward PlayerActions when you want to disable movement and unbind it when you want to reenable movement in those directions. The articles and search queries linked in my post should help point you towards the means of doing this - you can use those to assemble your system together.
How would I do the bindings exactly? Im kinda new to that contextplayeraction thingz and I saw u need to pass a handle function thingy and set a priority number and i have no idea what to do basically when it comes to creating these binds
ok I figured out how to do it but there is an issue, for some reason it stops every action made before it executes, is there a way for it to not do that? so for if an example i have w held down the char will walk forward even after that change without me having the click again?