想知道游戏中循环 while 如何运作

这段代码在游戏刚开始时在地图上生成了 100 只鸡。但是一旦玩家收集了所有 100 只鸡,就没有鸡可以收集了,所以我想知道当剩余数量降至 10 时如何再次生成 100 只鸡,使玩家始终有一定量的鸡可以收集。希望这样能让人理解,提前感谢。jp

local newChicken = game.ServerStorage:FindFirstChild("ChickenOnePart")

local TopLeftCorner = Vector3.new(-187.64, 20.679, 106.2)
local BottomRightCorner = Vector3.new(201.12, 20.679, -241.45)
local numberOfChickens = 100
local counter = 0

local singleTonRandom = Random.new(tick())

local function GetRandom(Min,Max)
return singleTonRandom:NextNumber(Min,Max)
end

while counter < numberOfChickens  do
local chicken = newChicken:Clone()
chicken.Anchored = true
chicken.Parent = game.Workspace
chicken.Name = "Chicken"
chicken.Position = Vector3.new(
    GetRandom(TopLeftCorner.X,BottomRightCorner.X),
    4,
    GetRandom(TopLeftCorner.Z,BottomRightCorner.Z)
)
counter = counter + 1

end

这段代码会在玩家触碰鸡时将 counter 加一。我可以从这里获取当前鸡数量吗?

if hitPart.Name == "Chicken" then
    event:FireServer(hitPart)
    local currentcount = playerGUI.ChickenGui.ChickenCounter.Text
    playerGUI.ChickenGui.ChickenCounter.Text = currentcount + 1
    hitPart:Destroy()
    debounce = true
    wait(0.1)
    debounce  = false
end
end)

这段代码用于更新玩家的领袖板。

local event = Instance.new("RemoteEvent")
event.Name = "CurrencyAdd"
event.Parent = game.ReplicatedStorage

event.OnServerEvent:Connect(function(plr,part)
local currency = plr.leaderstats.Chickens
currency.Value = currency.Value + 1
end)

game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
local m = Instance.new("IntValue",ls)
m.Name = "Chickens"
ls.Parent = plr

local cash = Instance.new("IntValue",ls)
cash.Name = "Cash"
end)

@Piglet 谢谢,我有点明白了,这是你的意思吗?

local newChicken = game.ServerStorage:FindFirstChild("ChickenOnePart")

local TopLeftCorner = Vector3.new(-187.64, 20.679, 106.2)
local BottomRightCorner = Vector3.new(201.12, 20.679, -241.45)
local numberOfChickens = 100
local counter = 0

local singleTonRandom = Random.new(tick())

local function GetRandom(Min,Max)
return singleTonRandom:NextNumber(Min,Max)
end

for counter = 1, numberOfChickens do
local chicken = newChicken:Clone()
chicken.Anchored = true
chicken.Parent = game.Workspace
chicken.Name = "Chicken"
chicken.Position = Vector3.new(
    GetRandom(TopLeftCorner.X,BottomRightCorner.X),
    4,
    GetRandom(TopLeftCorner.Z,BottomRightCorner.Z)
)
counter = counter + 1
end

function AddChicken(num)
for count = 10, num do
local chicken = newChicken:Clone()
chicken.Anchored = true
chicken.Parent = game.Workspace
chicken.Name = "Chicken"
chicken.Position = Vector3.new(
  GetRandom(TopLeftCorner.X,BottomRightCorner.X),
  4,
  GetRandom(TopLeftCorner.Z,BottomRightCorner.Z)
)
end
end
AddChicken(90)

很抱歉,我正在学习。

点赞
用户13691762
用户13691762

基本上,将该 while 循环放入自己的函数中,然后在鸡的数量低于一定阈值时调用该函数,就像调用 GetRandom 将鸡放置在位置上一样:

function GenerateChickens()
    while counter < numberOfChickens  do
        local chicken = newChicken:Clone()
        chicken.Anchored = true
        chicken.Parent = game.Workspace
        chicken.Name = "Chicken"
        chicken.Position = Vector3.new(
            GetRandom(TopLeftCorner.X,BottomRightCorner.X),
            4,
            GetRandom(TopLeftCorner.Z,BottomRightCorner.Z)
        )
        counter = counter + 1
    end
end

我不知道如何获取当前鸡的数量,但这应该是一个很好的开始。

2020-07-28 04:58:37
用户2858170
用户2858170

由于像这样的循环非常常见,因此比以下形式更短:

local counter = 0
while counter < numberOfChickens do
  -- ...
  counter = counter + 1
end

可以使用数值 for 循环来编写

for counter = 1, numberOfChickens do
  -- ...
end

如你所建议的当只剩下 10 只鸡时,添加另外的 100 只鸡,将给你 110 只鸡。因此,如果你想要地图上有 100 只鸡,你应该添加 90 只鸡。

要做到这一点,在删除鸡只时应该检查鸡只的数量。一旦你的条件成立,你就应该调用一个添加一定数量鸡只的函数。

function AddChicken(num)
  for count = 1, num do
    local chicken = newChicken:Clone()
    chicken.Anchored = true
    chicken.Parent = game.Workspace
    chicken.Name = "Chicken"
    chicken.Position = Vector3.new(
      GetRandom(TopLeftCorner.X,BottomRightCorner.X),
      4,
      GetRandom(TopLeftCorner.Z,BottomRightCorner.Z)
    )
  end
end

只需确保函数在 newChicken 的范围内。

2020-07-28 10:48:19