How do I make the spawner wait untill another "crop" can spawn again?

How do I add a wait on the spawner to spawn a crop, and I have a scythe u can take the crop from, how can I also make it check if the crop is gone and spawn again a minute later. Thank you for your help!

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
1 Like

There’s a way to do this, such as creating a value named as “newCrop”, and check if that value have a existing “Crop” linked to. This way, we will know whether or not a “Crop” is spawned or taken away

Also, if there are many spawnpoint for crops in “crop_spawn”, i think you should put the values inside the for i, v loop in order to make this work properly

local crop_spawn = game.Workspace.Crop_Spawn:GetChildren()
local crop = game.ReplicatedStorage.Crops.Wheat


for i, v in ipairs(crop_spawn) do
    local canSpawn = true
    local newCrop -- the instance to the crop
    while true do
    wait(1) -- prevent performance loss. You can use smaller value for the wait() to check more frequent but that will lose some performance if this is repeated on many other scripts
	if canSpawn == true and newCrop == nil then -- check whether or not the crop exists, in this case, spawned
        canSpawn = false
        wait(60) -- wait for 1 minute
		local newCrop = crop:Clone()
		newCrop.Name = "Crop"
		newCrop.Parent = workspace
		newCrop.Position = v.Position
        newCrop:GetPropertyChangedSignal("Parent"):Connect(function() -- check if the crop was taken away
            newCrop = nil
            canSpawn = true
            end)
	    end
    end
end

Edit : I forgot to put everything in a while true do loop, without this loop, this script will only work for once.

1 Like
local spawnDelay = 60  -- 60 seconds (1 minute) delay

while true do
    for i, v in ipairs(crop_spawn) do
        if canSpawn then
            canSpawn = false
            local newCrop = crop:Clone()
            newCrop.Name = "Crop"
            newCrop.Parent = game.Workspace
            newCrop.Position = v.Position
        end
    end
    wait(spawnDelay)  -- Wait for the specified delay before respawning
end
  1. Implement a check to see if the crop has been harvested and respawn it later:
-- In another part of your script, you can check if the crop is still in the workspace:
while true do
    for i, v in ipairs(game.Workspace:GetChildren()) do
        if v.Name == "Crop" and not v:IsDescendantOf(game.Players:GetChildren()) then
            v:Destroy()  -- Remove the crop
            canSpawn = true  -- Allow spawning a new crop
        end
    end
    wait(1)  -- Check every second if the crop is gone
end

This didn’t work. it says its because new crop is nil and hasnt been assigned

excuse for the long hold up, i just got home. I figure out that i have made a small mistake in there

if canSpawn and newCrop ~= nil then

to

if canSpawn == true and newCrop == nil then

Probably you already fixed this by yourself, but yeah.

yeah i fixed it in an easier way but I cant explain it very much because i just woke up lol

1 Like