修复Roblox Lua中'attempted to call a nil value'错误
问题与解决
问题详情
我正在制作一个ModuleScript,需要读取VehicleSeat的MaxSpeed、Torque、ThrottleFloat、SteerFloat和TurningSpeed属性。尽管座椅实际存在,且Studio显示目标属性可用,但仍触发“attempted to call a nil value”错误,且无代码位置提示。经排查,错误在读取Steer和Throttle属性时出现。
测试服务器脚本读取这些属性可正常运行,且该ModuleScript此前也能正常工作。尝试将属性作为函数参数传递仍报错,但直接传入数值则可正常运行。
相关代码:
调用脚本
local Controller = require(script.Parent.BasicSedanDoorController) Controller.DriverSeat.Changed:Connect(Controller.Drive())
ModuleScript
local Controller = {} vehicle = script.Parent Controller.DriverSeat = vehicle.body.DriverSeat.VehicleSeat suspension = vehicle.Suspension turningRadius = 45 Parts = { Steering = { FL = suspension.FrontLeftArm.Hub.CylindricalConstraint, FR = suspension.FrontRightArm.Hub.CylindricalConstraint }, driveTrain = { RL = suspension.RearLeftArm.Hub.MotorConstraint, RR = suspension.RearRightArm.Hub.MotorConstraint }, Lights = { } } topSpeed = Controller.DriverSeat.MaxSpeed Parts.driveTrain.RL.MotorMaxTorque = Controller.DriverSeat.Torque Parts.driveTrain.RR.MotorMaxTorque = Controller.DriverSeat.Torque Parts.Steering.FL.AngularSpeed = Controller.DriverSeat.TurnSpeed Parts.Steering.FR.AngularSpeed = Controller.DriverSeat.TurnSpeed Parts.Steering.FL.ServoMaxTorque = 1000000 Parts.Steering.FR.ServoMaxTorque = 1000000 function Controller.DoorController(door:Model) local servo: HingeConstraint = door:WaitForChild("Exterior"):WaitForChild("Servo") if servo.TargetAngle == 180 then servo.TargetAngle = 120 else servo.TargetAngle = 180 end end function Controller.Drive() Parts.Steering.FL.TargetAngle = Controller.DriverSeat.Steer Parts.Steering.FR.TargetAngle = Controller.DriverSeat.Steer Parts.driveTrain.RL.AngularVelocity = Controller.DriverSeat.Throttle Parts.driveTrain.RR.AngularVelocity = Controller.DriverSeat.Throttle end return Controller
错误原因
- 事件连接错误:
Controller.DriverSeat.Changed:Connect(Controller.Drive())中,Controller.Drive()是直接执行函数,而非传递函数引用。脚本运行时会立刻调用Drive(),若此时对象未完全初始化,函数返回nil,导致:Connect()接收到nil值,触发“attempted to call a nil value”错误。 - 属性名错误:VehicleSeat的转向、油门属性实际为
SteerFloat和ThrottleFloat,而非代码中写的Steer和Throttle,这会导致读取属性时返回nil。
修正方案
1. 修正事件连接
去掉Drive()的括号,传递函数引用给:Connect():
local Controller = require(script.Parent.BasicSedanDoorController) Controller.DriverSeat.Changed:Connect(Controller.Drive)
2. 修正属性名并添加空值检查
在Drive()函数中替换正确的属性名,并添加空值检查避免加载顺序问题:
function Controller.Drive() local steerValue = Controller.DriverSeat.SteerFloat local throttleValue = Controller.DriverSeat.ThrottleFloat if steerValue and throttleValue then Parts.Steering.FL.TargetAngle = steerValue Parts.Steering.FR.TargetAngle = steerValue Parts.driveTrain.RL.AngularVelocity = throttleValue Parts.driveTrain.RR.AngularVelocity = throttleValue end end
3. 优化变量声明(可选)
将全局变量改为local,避免污染全局环境:
local vehicle = script.Parent local suspension = vehicle.Suspension local turningRadius = 45 local Parts = { -- ... 原有内容 } local topSpeed = Controller.DriverSeat.MaxSpeed
完整修正后的ModuleScript
local Controller = {} local vehicle = script.Parent Controller.DriverSeat = vehicle.body.DriverSeat.VehicleSeat local suspension = vehicle.Suspension local turningRadius = 45 local Parts = { Steering = { FL = suspension.FrontLeftArm.Hub.CylindricalConstraint, FR = suspension.FrontRightArm.Hub.CylindricalConstraint }, driveTrain = { RL = suspension.RearLeftArm.Hub.MotorConstraint, RR = suspension.RearRightArm.Hub.MotorConstraint }, Lights = { } } local topSpeed = Controller.DriverSeat.MaxSpeed Parts.driveTrain.RL.MotorMaxTorque = Controller.DriverSeat.Torque Parts.driveTrain.RR.MotorMaxTorque = Controller.DriverSeat.Torque Parts.Steering.FL.AngularSpeed = Controller.DriverSeat.TurnSpeed Parts.Steering.FR.AngularSpeed = Controller.DriverSeat.TurnSpeed Parts.Steering.FL.ServoMaxTorque = 1000000 Parts.Steering.FR.ServoMaxTorque = 1000000 function Controller.DoorController(door: Model) local servo: HingeConstraint = door:WaitForChild("Exterior"):WaitForChild("Servo") if servo.TargetAngle == 180 then servo.TargetAngle = 120 else servo.TargetAngle = 180 end end function Controller.Drive() local steerValue = Controller.DriverSeat.SteerFloat local throttleValue = Controller.DriverSeat.ThrottleFloat if steerValue and throttleValue then Parts.Steering.FL.TargetAngle = steerValue Parts.Steering.FR.TargetAngle = steerValue Parts.driveTrain.RL.AngularVelocity = throttleValue Parts.driveTrain.RR.AngularVelocity = throttleValue end end return Controller
内容的提问来源于stack exchange,提问作者doge guide
相关产品推荐
相关产品推荐

