How to fix NPCs not spawning

元气小坏坏 提交于 2021-01-29 15:27:23

问题


I coded out some functions in a ModuleScript to be executed by another script. Here is the code

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

What I expected was the NPC trollanoids to appear on the map, but instead I got this error in the output:

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

I dont know what I did wrong, please help me fix this. Any help is appreciated


回答1:


The error message is telling you what's wrong:

You're trying to call an object. The only things you can call in Lua are functions and objects with the __call metamethod.




回答2:


You are calling an object. Like mentioned above, you can only call functions and objects with __call metamethod.

Try this:

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

To use the module you would do this:

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

I made a few minor changes. Let me know if you need help with any :)



来源:https://stackoverflow.com/questions/65436688/how-to-fix-npcs-not-spawning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!