我有一块草莓地,我希望每个人都能在其中产卵。我正在努力做到这一点,所以田地里一次只能有 10 个草莓。我已经将其设置为每次从 ReplicatedStorage 克隆草莓时,它都会放入工作区中,并且作为克隆 LocalScript 的子级的 IntValue 将增加 +1。克隆过程将一直持续到 IntValue 值为 10。当玩家拿起草莓时,IntValue 将减少 -1,从而生成新的草莓。问题是,除了 IntValue 增加 +1 之外,一切正常。这意味着 IntValue 将达到负数,草莓将永远生成。 StarterPlayerScripts 中的本地克隆脚本:
local remoteEvent = game.ReplicatedStorage.StrawberryCollect
local strawberryTemplate = game.ReplicatedStorage.Fruits.Strawberry
local function createStrawberryForPlayer()
local player = game.Players.LocalPlayer
local inventory = player:FindFirstChild("Inventory")
if inventory then
local strawberryCount = script.FruitAmount
if not strawberryCount or (strawberryCount and strawberryCount.Value < 10) then
local newStrawberry = strawberryTemplate:Clone()
newStrawberry.Parent = game.Workspace.Fruit.OnTheGround
newStrawberry.Position = Vector3.new(math.random(-163.146, -137.78), 2.5, math.random(-77.756, 8.488))
newStrawberry.Touched:Connect(function(otherPart)
local character = otherPart.Parent
if character and character:FindFirstChild("Humanoid") then
game.Workspace.SoundPack.Pickup:Play()
remoteEvent:FireServer(player)
newStrawberry:Destroy()
strawberryCount.Value = strawberryCount.Value + 1
end
end)
end
end
end
local function initialSpawn()
local player = game.Players.LocalPlayer
local strawberryCount = script.FruitAmount
if not strawberryCount then
strawberryCount = Instance.new("IntValue")
strawberryCount.Name = "FruitAmount"
strawberryCount.Parent = script
end
strawberryCount.Value += 1
createStrawberryForPlayer()
while true do
wait(math.random(10, 12))
createStrawberryForPlayer()
end
end
initialSpawn()
serverscriptserive 脚本,将 IntValue 值减少 -1:
local event = game.ReplicatedStorage.StrawberryCollect
event.OnServerEvent:Connect(function(plr)
game.StarterPlayer.StarterPlayerScripts["StrawberrySpawner3.0"].FruitAmount.Value -= 1
plr.Inventory.Strawberries.Value += 1
end)
欢迎任何建议或解释!
while true do
wait(math.random(10, 12))
if #workspace.Fruit.OnTheGround:GetChildren()==10 then continue end
createStrawberryForPlayer()
end