lua - 如何修复:产卵器不在僵尸中产卵
问题描述
我现在正在制作游戏,但我遇到了一个在僵尸中生成的脚本的问题。我不确定出了什么问题,但我觉得好像我的代码没有任何问题。就是这样,任何帮助表示赞赏。也许产生僵尸的替代解决方案会有所帮助。
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
解决方案
这是您的脚本中发生的事情:
- 声明一些变量
- 连接到信号
- 检查是否
spawneron == true
?不,跳过循环 - 完毕
当信号触发并且您设置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)
推荐阅读
- git - 如何从 git commit 中排除文件?
- javascript - 如何正确执行从 react-native 到 express.js 和 mySQL 后端的 POST 请求
- c - 编写 Linux 键盘驱动程序
- azure-devops - 错误:在 azure dev-ops(发布模式)中构建 UWP 移动应用时出现内部编译器错误
- android - Firebase 仪表板未显示顶级产品的价格
- javascript - 如何删除数组中的重复项?
- c# - 如何将格式为 Culture 和 UI Culture (c=nb-NO|uic=nb-NO) 的字符串解析为语言代码
- sql - 一次创建多个表
- spring - 表单提交成功后被多次调用
- oauth - 通过 OAuth2 使用 microsoft task api 时遇到问题