如何解决NPC未生成的问题

我编写了一些函数放在一个ModuleScript中,以供另一个脚本执行。以下是代码

local module = {}

wavepause = game.ReplicatedStorage.Values.WavePauseLength.Value
trollanoid = game.ReplicatedStorage.Trollanoid
spawnpoints = workspace.Test1.Spawns:GetChildren()

function trollanoidsummon()
    local chosenspawn = math.random(#spawnpoints)
    local clone = trollanoid:Clone().Parent == workspace.Zombies
    clone.HumanoidRootPart.CFrame = chosenspawn.CFrame
end

module.Wave1 = function()
    trollanoid()
    wait(1)
    trollanoid()
    wait(1)
    trollanoid()
    wait(1)
    trollanoid()
end

return module

我期望NPC trollanoid出现在地图上,但实际上在输出中遇到了如下错误:

17:50:19.011  ServerScriptService.WaveModule:14: attempt to call a Instance
value  -  Server  -  WaveModule:14

我不知道我做错了什么,请帮助我修复这个问题。非常感谢您的帮助

点赞
用户4984564
用户4984564

错误信息是告诉你哪里出了问题:

您正在尝试调用一个对象。在 Lua 中,您只能调用_函数_和带有__call元方法的对象。

2020-12-24 10:19:16
用户14208240
用户14208240

你正在调用一个对象。如上所述,你只能使用 __call 元方法来调用函数和对象。

请尝试:

local module = {}

wavepause = game.ReplicatedStorage.Values.WavePauseLength
trollanoid = game.ReplicatedStorage.Trollanoid
spawnpoints = workspace.Test1.Spawns:GetChildren()

function trollanoidsummon()
    local chosenspawn = spawnpoints[math.random(#spawnpoints)]
    local clone = trollanoid:Clone().Parent = workspace.Zombies
    clone.HumanoidRootPart.CFrame = chosenspawn.CFrame
end

module:SpawnNPC(amount, threshold)
    threshold = threshold or 1
    amount = amount or 4
    for i = 1, amount do
        if wavepause.Value then break end;
        trollanoidsummon()
        wait(threshold)
    end
end

return module

要使用该模块,您需要这样做:

local spawner = require(modulescriptpath);
spawner:SpawnNPC(5, 1);

我进行了一些小小的修改。如果需要帮助,请告诉我 :)

2020-12-24 12:51:14