从 Roblox 教程中得到的 Punch 动画出现错误

我正在试图从教程中获取 Punch 动画,但是每当我按下指定的绑定键时它从未起作用。

我已经尝试将教程中的值从 1 更改为动画长度,甚至是超过动画长度和随机数字。我还尝试更改了脚本的一些措辞。

math.randomseed(tick())

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"

local animation = (03910055905)

local function onPunchFired(plr)
    local char = game.Workspace:FindFirstChild(plr.Name)
    local humanoid = char.Humanoid
    local animation = Instance.new("Animation")
    animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
    local animTrack = humanoid:LoadAnimation(animation)
    animTrack:Play()
end

punchEvent.OnServerEvent:Connect(onPunchFired)

我期望脚本能够平稳运行并且打出拳,但是它显示了以下内容:

20:46:29.496 - ServerScriptService.ExtremePunch:13: attempt to get length of global 'animations' (a nil value)

这是它在错误输出中显示的所有内容。我已经再次检查了复制,但它仍然失败了。我该如何解决?

点赞
用户2860267
用户2860267

你的错误指向了这一行:

animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]

这是因为在这个脚本的范围内,没有名为animations的本地变量或数组。

要修复你的代码,你只需要修正animation.AnimationId的网址中的数字。你可能遇到的一个问题是,你有两个名为animation的变量,因此它在正确构建网址时失败了。

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"

local function onPunchFired(plr)
    local char = game.Workspace:FindFirstChild(plr.Name)
    local humanoid = char.Humanoid
    local animation = Instance.new("Animation")
    animation.AnimationId = "rbxassetid://3910055905"
    local animTrack = humanoid:LoadAnimation(animation)
    animTrack:Play()
end

punchEvent.OnServerEvent:Connect(onPunchFired)

如果你不确定你的资产id是否正确,你也可以将其放入Roblox目录的网址中进行检查:https://www.roblox.com/catalog/YOUR_ASSET_ID_NUMBER。如果它重定向到你的动画,你就知道你选对了。

2019-09-17 02:59:08
用户11815974
用户11815974
`animations` 列表在你给我们展示的代码中并不存在。因此,将你的其他拳击动画添加到我添加的 `animations` 列表中,你就可以开始使用了。我没有测试过,如果有问题请和我联系。

local replicatedStorage = game:GetService("ReplicatedStorage") local punchEvent = Instance.new("RemoteEvent") punchEvent.Name = "PunchEvent" punchEvent.Parent = replicatedStorage

local animations = [03910055905, ...]

local function onPunchFired(plr) local char = game.Workspace:FindFirstChild(plr.Name) local humanoid = char.Humanoid local animation = Instance.new("Animation") animation.AnimationId = "rbxassetid://" .. tostring(animations[math.random(1, #animations)])

local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()

end

punchEvent.OnServerEvent:Connect(onPunchFired) ```

2019-10-04 15:09:51