如何修复:刷怪器在僵尸中未刷出

我正在制作一个游戏,我的一个脚本在刷出僵尸时出现了问题。我不确定出了什么问题,但我觉得我的代码中没有任何问题。以下是代码,任何帮助都将不胜感激。也许刷出僵尸的替代解决方案会有所帮助。

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
点赞
用户2860267
用户2860267

以下是脚本中发生的情况:

  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)
2020-12-22 05:31:35