I want to use for loops to shorten this code but I’m not sure how.
function MovementController.Enable()
CharacterTrove:Connect(Players.LocalPlayer.CharacterAdded, function(character: Model)
Character = character
Humanoid = character.Humanoid :: Humanoid
connectHumanoid() -- need to run this again because the character is now a new instance
end)
connectHumanoid()
Trove:Connect(Players.PlayerRemoving, function(Player: Player)
if Player == Players.LocalPlayer then
Trove:Destroy()
CharacterTrove:Destroy()
end
end)
-- desktop
Trove:Connect(UserInputService.InputBegan, function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then
return
end
for _, Key in ipairs(ANIMATION_IDS["Walk"].Keys.Left) do
if input.KeyCode == Key then
Humanoid:Move(Vector3.new(-1, 0, 0))
return
end
end
for _, Key in ipairs(ANIMATION_IDS["Walk"].Keys.Right) do
if input.KeyCode == Key then
Humanoid:Move(Vector3.new(1, 0, 0))
return
end
end
for _, Key in ipairs(ANIMATION_IDS["Jump"].Keys) do
if input.KeyCode == Key and Humanoid.FloorMaterial ~= Enum.Material.Air then -- needs to be grounded
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
return
end
end
end)
Trove:Connect(UserInputService.InputEnded, function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then
return
end
if
table.find(ANIMATION_IDS["Walk"].Keys.Left, input.KeyCode)
or table.find(ANIMATION_IDS["Walk"].Keys.Right, input.KeyCode)
then
-- if the player is still holding the opposite key, don't stop movement
for _, Key in ANIMATION_IDS["Walk"].Keys.Left do
if input.KeyCode == Key and Humanoid.MoveDirection.X == 1 then
return
end
end
for _, Key in ANIMATION_IDS["Walk"].Keys.Right do
if input.KeyCode == Key and Humanoid.MoveDirection.X == -1 then
return
end
end
Humanoid:Move(Vector3.new(0, 0, 0))
end
end)
-- mobile
if UserInputService.TouchEnabled then
local TouchUI = Touch.CreateTouchUI()
local LeftButton = TouchUI.TouchControlFrame.LeftButton
local RightButton = TouchUI.TouchControlFrame.RightButton
local JumpButton = TouchUI.TouchControlFrame.JumpButton
Trove:Connect(LeftButton.InputBegan, function()
LeftButton.ImageColor3 = Touch.PRESSED_COLOR
Humanoid:Move(Vector3.new(-1, 0, 0))
end)
Trove:Connect(RightButton.InputBegan, function()
RightButton.ImageColor3 = Touch.PRESSED_COLOR
Humanoid:Move(Vector3.new(1, 0, 0))
end)
Trove:Connect(LeftButton.InputEnded, function()
LeftButton.ImageColor3 = Color3.new(1, 1, 1)
Humanoid:Move(Vector3.new(0, 0, 0))
end)
Trove:Connect(RightButton.InputEnded, function()
RightButton.ImageColor3 = Color3.new(1, 1, 1)
Humanoid:Move(Vector3.new(0, 0, 0))
end)
Trove:Connect(JumpButton.InputBegan, function()
if Humanoid.FloorMaterial ~= Enum.Material.Air then -- needs to be grounded
JumpButton.ImageColor3 = Touch.PRESSED_COLOR
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end)
Trove:Connect(JumpButton.InputEnded, function()
JumpButton.ImageColor3 = Color3.new(1, 1, 1)
end)
end
end
function MovementController.Enable()
-- avoid writing "Vector3.new" everywhere
local function SetMovement(Direction)
Humanoid:Move(Vector3.new(Direction, 0, 0))
end
local function SetupCharacter(NewCharacter)
Character = NewCharacter
-- :WaitForChild is safer
Humanoid = NewCharacter:WaitForChild("Humanoid")
ConnectHumanoid() -- :shrug:
end
-- helper to check if any key in a key group (like all Left keys) is held
-- this is needed to fix the bug where holding two keys stops the player
local function IsAnyKeyDown(Keys)
for _, KeyCode in Keys do
if not UserInputService:IsKeyDown(KeyCode) then continue end
return true
end
return false
end
CharacterTrove:Connect(Players.LocalPlayer.CharacterAdded, SetupCharacter)
if Players.LocalPlayer.Character then SetupCharacter(Players.LocalPlayer.Character) end
Trove:Connect(Players.PlayerRemoving, function(Player)
if Player ~= Players.LocalPlayer then return end
Trove:Destroy()
CharacterTrove:Destroy()
end)
Trove:Connect(UserInputService.InputBegan, function(Input, Processed)
if Processed then return end
local KeyCode = Input.KeyCode
if table.find(ANIMATION_IDS.Walk.Keys.Left, KeyCode) then SetMovement(-1) return end
if table.find(ANIMATION_IDS.Walk.Keys.Right, KeyCode) then SetMovement(1) return end
if table.find(ANIMATION_IDS.Jump.Keys, KeyCode) and Humanoid.FloorMaterial ~= Enum.Material.Air then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end)
Trove:Connect(UserInputService.InputEnded, function(Input, Processed)
if Processed then return end
local KeyCode = Input.KeyCode
local IsLeftKey = table.find(ANIMATION_IDS.Walk.Keys.Left, KeyCode)
local IsRightKey = table.find(ANIMATION_IDS.Walk.Keys.Right, KeyCode)
if not IsLeftKey and not IsRightKey then return end
-- check what keys are CURRENTLY held down.
local HoldingLeft = IsAnyKeyDown(ANIMATION_IDS.Walk.Keys.Left)
local HoldingRight = IsAnyKeyDown(ANIMATION_IDS.Walk.Keys.Right)
-- if releasing a key but the opposite is still held, switch direction
if IsLeftKey and HoldingRight then SetMovement(1) return end
if IsRightKey and HoldingLeft then SetMovement(-1) return end
-- if releasing a key but still holding another key for the same direction, do nothing
if IsLeftKey and HoldingLeft then return end
if IsRightKey and HoldingRight then return end
-- otherwise stop
SetMovement(0)
end)
if not UserInputService.TouchEnabled then return end
local UserInterface = Touch.CreateTouchUI().TouchControlFrame
-- using a configuration table prevents repeating code 4 times.
local Configurations = {
{["Button"] = UserInterface.LeftButton, ["Direction"] = -1};
{["Button"] = UserInterface.RightButton, ["Direction"] = 1};
{["Button"] = UserInterface.JumpButton, ["IsJump"] = true};
}
for _, Configuration in Configurations do
Trove:Connect(Configuration.Button.InputBegan, function()
Configuration.Button.ImageColor3 = Touch.PRESSED_COLOR
-- jump
if Configuration.IsJump and Humanoid.FloorMaterial ~= Enum.Material.Air then
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
return
end
-- movement
if not Configuration.Direction then return end
SetMovement(Configuration.Direction)
end)
Trove:Connect(Configuration.Button.InputEnded, function()
Configuration.Button.ImageColor3 = Color3.new(1, 1, 1)
if not Configuration.Direction then return end
SetMovement(0)
end)
end
end
(don’t paste this and expect it to work first try)