Hello, devs. I want to rescript Roblox movement system. (And possibly make it better). But, when I tried to test if smth even work, I got error which I never saw earlier:
Module code did not return exactly one value
What can cause such error?
Codes
ControlModule
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local VRService = game:GetService("VRService")
local Player = Players.LocalPlayer
local Modules = {
Keyboard = script.Keyboard,
VR = script.VR,
}
local ControlModule = {}
ControlModule.__index = ControlModule
function ControlModule.Inititalize()
local InitialInfo = {
CurrentCharacter = nil,
CurrentHumanoid = nil,
MoveDirection = Vector3.new(0, 0, 0), -- Y is for swimming
Speed = 1, --Used for run, multiplies with walkspeed skill
JumpState = 0,
MoveFunction = nil,
Device = nil,
ActiveModule = nil,
CurrentMovementState = nil,
}
local self = setmetatable(InitialInfo, ControlModule)
local Device
if UserInputService.KeyboardEnabled then
if UserInputService.MouseEnabled then
Device = "Keyboard&Mouse"
else
Device = "Keyboard"
end
elseif UserInputService.TouchEnabled then
Device = "TouchScreen" --impossible I guess
elseif UserInputService.GamepadEnabled then
Device = "Joystick"
end
self:ChangeControlsType(Device)
end
function ControlModule:ChangeControlsType(ControlsType)
if self.Device == ControlsType then return end
if ControlsType == "Keyboard" or ControlsType == "Keyboard&Mouse" then
self.ActiveModule = require(Modules.Keyboard)
end
self.ActiveModule:Initialize()
self.ActiveModule:Enable(true)
end
local function CalculateMoveDirection(MoveState, InitialDirection)
local Camera = workspace.CurrentCamera
if not Camera then return InitialDirection end
if MoveState == "Swim" then
return Camera.CFrame:VectorToWorldSpace(InitialDirection)
end
local CameraCFrame = Camera.CFrame
if VRService.VREnabled then
local VRFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
CameraCFrame = CameraCFrame * VRFrame
end
local c, s
local _, _, _, R00, R01, R02, _, _, R12, _, _, R22 = CameraCFrame:GetComponents()
if R12 < 1 and R12 > -1 then
c = R22
s = R02
else
c = R00
s = -R01*math.sign(R12)
end
local norm = math.sqrt(c*c + s*s)
return Vector3.new((c * InitialDirection.X + s * InitialDirection.Z) / norm, 0, (c *InitialDirection.Z - s * InitialDirection.X) / norm)
end
function ControlModule:OnRenderStepped(dt)
if self.activeController and self.activeController.enabled and self.humanoid then
-- Give the controller a chance to adjust its state
self.activeController:OnRenderStepped(dt)
-- Now retrieve info from the controller
local moveVector = self.activeController:GetMoveVector()
local cameraRelative = self.activeController:IsMoveVectorCameraRelative()
-- Are we driving a vehicle ?
local vehicleConsumedInput = false
if self.CurrentMovementState == "Vehicle" then
moveVector, vehicleConsumedInput = self.vehicleController:Update(moveVector, cameraRelative, self.activeControlModule==Gamepad)
end
-- If not, move the player
-- Verification of vehicleConsumedInput is commented out to preserve legacy behavior,
-- in case some game relies on Humanoid.MoveDirection still being set while in a VehicleSeat
--if not vehicleConsumedInput then
if cameraRelative then
moveVector = CalculateMoveDirection(self.CurrentMovementState, moveVector)
end
Player:Move(moveVector, false)
--end
-- And make them jump if needed
self.humanoid.Jump = self.activeController:GetIsJumping() or (self.touchJumpController and self.touchJumpController:GetIsJumping())
end
end
return ControlModule.Inititalize()
Keyboard module
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local ActionPriorityLevel = 50
local Keyboard = {}
Keyboard.__index = Keyboard
function Keyboard.Initialize(Enabled)
local InitializationTable = {
Enabled = false,
MoveVector = Vector3.zero,
MoveVectorIsCameraRelative = true,
TextFocusReleasedConn = nil,
TextFocusGainedConn = nil,
WindowFocusReleasedConn = nil,
ForwardValue = 0,
BackwardValue = 0,
LeftValue = 0,
RightValue = 0,
JumpEnabled = false,
}
local self = setmetatable(InitializationTable, Keyboard)
return self
end
function Keyboard:Enable(enable)
if not UserInputService.KeyboardEnabled then
return false
end
if enable == self.Enabled then
return true
end
self.ForwardValue = 0
self.BackwardValue = 0
self.LeftValue = 0
self.RightValue = 0
self.MoveVector = Vector3.zero
self.JumpRequested = false
self.isJumping = false
if enable then
self:BindActions()
self:ConnectFocusEvents()
else
self:UnbindActions()
self:DisconnectFocusEvents()
end
self.Enabled = enable
return true
end
function Keyboard:BindActions()
local handleMoveForward = function(actionName, inputState, inputObject)
self.ForwardValue = (inputState == Enum.UserInputState.Begin) and -1 or 0
self:UpdateMovement(inputState)
return Enum.ContextActionResult.Pass
end
local handleMoveBackward = function(actionName, inputState, inputObject)
self.BackwardValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
self:UpdateMovement(inputState)
return Enum.ContextActionResult.Pass
end
local handleMoveLeft = function(actionName, inputState, inputObject)
self.LeftValue = (inputState == Enum.UserInputState.Begin) and -1 or 0
self:UpdateMovement(inputState)
return Enum.ContextActionResult.Pass
end
local handleMoveRight = function(actionName, inputState, inputObject)
self.RightValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
self:UpdateMovement(inputState)
return Enum.ContextActionResult.Pass
end
local handleJumpAction = function(actionName, inputState, inputObject)
self.JumpRequested = self.JumpEnabled and (inputState == Enum.UserInputState.Begin)
self.isJumping = self.jumpRequested
return Enum.ContextActionResult.Pass
end
ContextActionService:BindActionAtPriority("ForwardAction", handleMoveForward, false,
ActionPriorityLevel, Enum.KeyCode.W)
ContextActionService:BindActionAtPriority("BackwardAction", handleMoveBackward, false,
ActionPriorityLevel, Enum.KeyCode.S)
ContextActionService:BindActionAtPriority("LeftAction", handleMoveLeft, false,
ActionPriorityLevel, Enum.KeyCode.A)
ContextActionService:BindActionAtPriority("RightAction", handleMoveRight, false,
ActionPriorityLevel, Enum.KeyCode.D)
ContextActionService:BindActionAtPriority("JumpAction", handleJumpAction, false,
ActionPriorityLevel, Enum.KeyCode.Space)
end
function Keyboard:UnbindActions()
ContextActionService:UnbindAction("ForwardAction")
ContextActionService:UnbindAction("BackwardAction")
ContextActionService:UnbindAction("LeftAction")
ContextActionService:UnbindAction("RightAction")
ContextActionService:UnbindAction("JumpAction")
end
function Keyboard:ConnectFocusEvents()
local function onFocusReleased()
self.moveVector = Vector3.zero
self.forwardValue = 0
self.backwardValue = 0
self.leftValue = 0
self.rightValue = 0
self.jumpRequested = false
self.isJumping = self.jumpRequested
end
local function onTextFocusGained(textboxFocused)
self.jumpRequested = false
self.isJumping = self.jumpRequested
end
self.TextBoxFocusRelease = UserInputService.TextBoxFocusReleased:Connect(onFocusReleased)
self.TextBoxFocusGain = UserInputService.TextBoxFocused:Connect(onTextFocusGained)
self.WindowFocusRelease = UserInputService.WindowFocusReleased:Connect(onFocusReleased)
self.WindowFocusGain = UserInputService.WindowFocused:Connect(onFocusReleased)
end
function Keyboard:DisconnectFocusEvents()
if self.TextBoxFocusRelease then
self.TextBoxFocusRelease:Disconnect()
self.TextBoxFocusRelease = nil
end
if self.TextBoxFocusGain then
self.TextBoxFocusGain:Disconnect()
self.TextBoxFocusGain = nil
end
if self.WindowFocusRelease then
self.WindowFocusRelease:Disconnect()
self.WindowFocusRelease = nil
end
if self.WindowFocusGain then
self.WindowFocusGain:Disconnect()
self.WindowFocusGain = nil
end
end
return Keyboard.Initialize()
I know that there’s probably a lot of mistakes in code for now, but I want know what can cause such error.