I have a crop spawner folder with numbered spawners 1-24 and I have a crop in replicated storage that needs to be cloned and put into workspace and I want every crop to spawn in 60 seconds. I also need a way to check if the crop had been harvested and needs to spawn again! Thank you for helping.
local crop_spawn = game.Workspace.Crop_Spawn:GetChildren()
local crop = game.ReplicatedStorage.Crops.Wheat
local canSpawn = true
for i, v in ipairs(crop_spawn) do
if canSpawn then
canSpawn = false
local newCrop = crop:Clone()
newCrop.Name = "Crop"
newCrop.Parent = workspace
newCrop.Position = v.Position
end
end
you can use a combination of loops and events. The key is to keep track of whether a crop is present at each spawn point and to respawn it when it’s harvested.
Here’s an updated version of your script:
local crop_spawn_points = game.Workspace.Crop_Spawn:GetChildren()
local crop_template = game.ReplicatedStorage.Crops.Wheat
local spawn_delay = 60
local function spawnCrop(spawn_point)
local newCrop = crop_template:Clone()
newCrop.Name = "Crop"
newCrop.Parent = workspace
newCrop.Position = spawn_point.Position
-- Listen for the crop being destroyed (harvested)
newCrop.AncestryChanged:Connect(function(child, parent)
if not parent then
-- Crop was destroyed, respawn after delay
wait(spawn_delay)
spawnCrop(spawn_point)
end
end)
end
local function initializeCrops()
for _, spawn_point in ipairs(crop_spawn_points) do
spawnCrop(spawn_point)
end
end
-- Start the crop spawning process
initializeCrops()
I fixed it in a way simpler way by making a function called hasCropAtPosition and checks it all, it is kinda complicated but it was alot less lines of codes and works great! Thanks for the help tho.