此代码在游戏开始时在某个区域内的地图上生成了100只鸡,但是玩家已经收集了全部100只鸡,没有剩余的要收集,所以id想知道如何重新生成另外100只鸡,说一次剩余数量已减少到10,因此玩家将有连续数量的鸡可以收集,希望这是有道理的,在此先感谢。 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
此代码每次玩家触摸鸡时将计数器加1。我可以从这里获得当前的鸡只数量吗?
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)
对不起,我只是学习全部
答案 0 :(得分:0)
基本上,只需将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
我不知道您将如何获得当前的鸡数量,但这应该是一个好的开始。
答案 1 :(得分:0)
因为这样的循环很常见,所以形式比以下更短:
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的范围内