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)
end
end
end)
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) + Vector3.new(0, workspace.Gravity * .5, 0))
task.wait(2)
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")
task.wait(1)
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")
task.wait(1)
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")
task.wait(1)
else
warn("Ground Pound Is On Cooldown.")
end
end
end
-- state manager
return module