在Roblox中出现错误“参数3丢失或为零”

问题描述

我正在用Roblox编码,遇到一个错误,告诉我参数3丢失或为零。这是我的代码

local tweenservice = game:GetService("TweenService")
local part = workspace.TruckBlock

local Tween = tweenservice:Create(part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,false,{Position = Vector3.new(5.642,1.65,-17.99)}))
wait(5)
Tween:Play()

我是一名新编码员,我真的不明白出了什么问题。

解决方法

该错误告诉您调用的函数需要三个参数,而您仅提供了两个参数。 TweenService:Create()期望目标,一些补间信息和道具字典发生变化。

您的代码具有所有正确的部分,但您在错误的位置加上了短括号:

e,{Position = Vector3.new(5.642,1.65,-17.99)}))
   ^ the ) needs to go here                      ^ not here

这会导致道具字典被传递到补间信息构造函数中。因此,不是TweenService:Create()得到三个参数,而是得到两个参数。

很难发现错误,因为您已将所有内容都放在一行上。将事情分解成多行后,它更易于阅读,错误消息也将更易于理解。

local tweenservice = game:GetService("TweenService")
local part = workspace.TruckBlock

local tweenInfo = TweenInfo.new(1,-- time
    Enum.EasingStyle.Linear,-- easing style
    Enum.EasingDirection.Out,-- easing direction
    0,-- repeat count
    false,-- reverses
    5)                             -- delay

local props = {
    Position = Vector3.new(5.642,-17.99),}

local Tween = tweenservice:Create(part,tweenInfo,props)
Tween:Play()