首页 > 解决方案 > 如何修复:产卵器不在僵尸中产卵

问题描述

我现在正在制作游戏,但我遇到了一个在僵尸中生成的脚本的问题。我不确定出了什么问题,但我觉得好像我的代码没有任何问题。就是这样,任何帮助表示赞赏。也许产生僵尸的替代解决方案会有所帮助。

local NPC = game.ReplicatedStorage.Trollanoid
local spawner = script.Parent
local spawneron = false
game.ReplicatedStorage.Values.gameInProgress.Changed:Connect(function()
    if game.ReplicatedStorage.Values:FindFirstChild("gameInProgress").Value == true then
        if game.ReplicatedStorage.Values:FindFirstChild("zombiesRemaining").Value > 0 then  
            spawneron = true
        end
    end
end)

while spawneron == true do
    local Clone = NPC:Clone()
    Clone.UpperTorso.CFrame = spawner.CFrame
    Clone.Parent = workspace
    game.ReplicatedStorage.Values:FindFirstChild("zombiesRemaining").Value =                     
game.ReplicatedStorage.Values:FindFirstChild("zombiesRemaining").Value - 1
    wait(3)
end

标签: luaroblox

解决方案


这是您的脚本中发生的事情:

  1. 声明一些变量
  2. 连接到信号
  3. 检查是否spawneron == true?不,跳过循环
  4. 完毕

当信号触发并且您设置spawneron为 true 时,您的代码不会重新进入并激活 while 循环。为此,只需将循环移动到函数中,并在信号触发时调用该函数。

local NPC = game.ReplicatedStorage.Trollanoid
local gameInProgress = game.ReplicatedStorage.Values.gameInProgress
local zombiesRemaining = game.ReplicatedStorage.Values.zombiesRemaining
local spawner = script.Parent

local function spawnZombies()
    while zombiesRemaining.Value > 0 do
        local Clone = NPC:Clone()
        Clone:SetPrimaryPartCFrame(spawner.CFrame)
        Clone.Parent = workspace
        zombiesRemaining.Value = zombiesRemaining.Value - 1
        wait(3)
    end
end

gameInProgress.Changed:Connect(function(newValue)
    if newValue == true then
        if zombiesRemaining.Value > 0 then  
            spawnZombies()
        end
    end
end)

推荐阅读