Feedback on messy movement module

Hello, I’m making multiple moves for the player in my indie platformer game that I will be releasing onto the platform. I’m curious on how I can make this messy looking code look more polished.

The code listed below consist of a local script that controls the keybinds and a playerModule that preforms actions on the player.

local UIS = game:GetService("UserInputService")
local moveModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Player"):WaitForChild("Moveset"))

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local state = "Freefalling" -- Base - Will be changed later
local moves = moveModule.moves

UIS.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.Space) then
			moves("LJ", char)
		elseif UIS:IsKeyDown(Enum.KeyCode.LeftControl) then	
			moves("Crouch", char)
		elseif UIS:IsKeyDown(Enum.KeyCode.E) then
			moves("Dive", char)
		elseif UIS:IsKeyDown(Enum.KeyCode.Tab) and state == "Freefalling" then -- don't mind the keybind, just a wip
			moves("GP", char)
local module = {}

module.LJCoolDown = false
module.CCooldown = false
module.DCooldown = false
module.GPCooldown = false

-- moves function
function module.moves(input, character)
	if input == "LJ" then
		if not module.LJCoolDown then
			module.LJCoolDown = true print("Long Jump Activated") 
			local root = character:FindFirstChild("HumanoidRootPart")
			root:ApplyImpulse((1250 * root.CFrame.LookVector) +, workspace.Gravity * .5, 0))
			module.LJCoolDown = false
		else warn("Long Jump Is On Cooldown.") end
	elseif input == "Crouch" then 
		if not module.CCooldown then
			module.CCooldown = true print("Crouch Activated")


			module.CCooldown = false
		else warn("Crouch Is On Cooldown.") end
	elseif input == "Dive" then
		if not module.DCooldown then
			module.DCooldown = true print("Dive Activated")


			module.DCooldown = false
		else warn("Dive Is On Cooldown.") end
	elseif input == "GP" then
		if not module.GPCooldown then
			module.GPCooldown = true print("Ground Pound Activated")

			warn("Ground Pound Is On Cooldown.")

-- state manager

return module

1 Like

I would modularize the movement instead of using nested and repetitive if-statements. I would also create a keybind map as a dictionary, and then use context action service to use that data and assign keybinds to actions.

1 Like

Why context action server over user input service?

ContextActionService provides greater flexibility and assistance with reducing code redundancy. Writing with UserInputService to do key binds increases code redundancy and isn’t as easily flexible for some things, such as combined keybinds, cross-platform support, etc.


The code is pretty good, but it could be a lot cleaner and easy to look at.

You can rewrite the module script based off of this and how I rewrote it:

-->> Services <<--
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-->> Remotes / Modules <<--
local MoveModule = require(ReplicatedStorage:WaitForChild("Player"):WaitForChild("Moveset"))

-->> Character <<--
local Character = game.Players.LocalPlayer.Character
local Humanoid = Character:FindFirstChild("Humanoid")
local CurrentState = "Freefalling" -- Base
local Moves = MoveModule.moves

-->> Input <<--

UserInputService.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and UserInputService:IsKeyDown(Enum.KeyCode.Space) then
			Moves("LJ", Character)
		elseif UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
			Moves("Crouch", Character)
		elseif UserInputService:IsKeyDown(Enum.KeyCode.E) then
			Moves("Dive", Character)
		elseif UserInputService:IsKeyDown(Enum.KeyCode.Tab) and CurrentState == "Freefalling" then
			Moves("GP", Character)
1 Like