Questions about custom movement scripts/character controllers and how they interact with default player scripts

  1. What do I want to achieve?

Nothing exact, more so wanted to here some thoughts about a basic movement script I created and how it interacts with other default scripts.

  1. What is the issue?

I was playing around with an idea and made this really basic movement script

local moveSpeed = 16
local canMove = true

-- Character Controller Functions
local function onMove(input)
	if not canMove then return end
	
	local moveVector = Vector3.new(input.x, 0, input.y)
	moveVector = moveVector.Unit * moveSpeed
	
	humanoid:Move(moveVector, false)
end

local function onInputBegan(input, isProcessed)
	if isProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		onMove(Vector2.new(0, 1))
	elseif input.KeyCode == Enum.KeyCode.S then
		onMove(Vector2.new(0, -1))
	elseif input.KeyCode == Enum.KeyCode.A then
		onMove(Vector2.new(-1, 0))
	elseif input.KeyCode == Enum.KeyCode.D then
		onMove(Vector2.new(1, 0))
	end
end

local function onInputEnd(input, isProcessed)
	if isProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then
		onMove(Vector2.new(0, 0))
	elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
		onMove(Vector2.new(0, 0))
	end
end

UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnd)

The canMove variable is only included because I wanted to be able to restrict movement to the player when interacting with NPCs in the world.

The script itself works fine, but it is definitely having some integration issues. It is interacting terribly with the default player movement scripts. I can tell because there is no jumping mechanic implemented into my script, and yet, I can jump in game. In addition to that, the movement ingame is extremely buggy, with there being a delay upon pressing WASD and my avatar almost phasing in and out of existence.

I don’t necessarily need help with this exact script, but I was curious as to whether or not it is even necessary to create a completely new character controller for something as simple as restricting character movement.

I tried another very simple solution…

local character = game.Players.LocalPlayer.Character

if character then
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
	end
end

I was just planning on tying this script (and another one to revert the values) into a ModuleScript. However, this just feels like bad practice, but I am unsure. At the end of the day, all I need to do is stop the player from moving for a little while.

Really I am just wondering if it is necessary to create an entirely new Character Controller for simple tasks. The way I am envisioning my current project, I will realistically only need perhaps one to two more extra inputs not included in the default character controller.

In addition to that, if I created a whole entirely new controller, I would have to change all the inputs for devices other than PC, which also seems like a pain to just restrict movement for a second.

I guess, in what situations would you truly need to create an entirely new Character Controller, and is there a simple way to restrict character movement to circumvent this?

Thanks in advance.

1 Like