【问题标题】:Punch animation from Roblox tutorial is throwing errorsRoblox 教程中的打孔动画引发错误
【发布时间】:2020-01-17 20:34:32
【问题描述】:

我正在尝试从教程中获得一个打孔动画,但是当我按下指定的键绑定时它永远不会工作。

我尝试将教程中的值从 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)

这就是它在错误输出中所说的全部内容。我已经仔细检查了复制,但它一直失败。我该如何解决?

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    你的错误指向这一行:

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

    这是因为在这个脚本的作用域内,没有名为animations的局部变量或数组。

    要修复您的代码,您只需修复 animation.AnimationId 网址中的数字即可。您可能遇到的一个问题是您有两个名为 animation 的变量,因此它无法为您正确构建 url。

    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 目录的 URL 中进行检查:https://www.roblox.com/catalog/YOUR_ASSET_ID_NUMBER。如果它重定向到您的动画,您就知道您找到了正确的动画。

    【讨论】:

    • 谢谢,帮了大忙。
    【解决方案2】:

    animations 列表在您向我们展示的代码中不存在。因此,将您的其他打孔动画添加到我添加的animations 列表中,您应该一切顺利。我没有测试它,所以如果它不起作用,HMU。

    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-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2022-07-21
      • 2022-12-21
      • 2012-10-16
      • 2011-07-28
      相关资源
      最近更新 更多