Script giving up after generating 5 corn

Hi! I’m making a new game about collecting corn. This is about corn generation.

I want a script to generate corn, but stop after a limit. Then, it waits until the player collects the corn and then generates more corn.

After reaching the limit, when the player collects the corn, the script gives up and stops generating corn. How can I fix this?

Script:

local SStorage = game:GetService("ServerStorage")
local corn = SStorage.corn
local cornamount = game:GetService("ReplicatedStorage").cornamount
cornamount = 0
local function addcorn()
	wait(8)
	local corn2 = corn:Clone()
	corn2.Parent = game.Workspace
	corn2.Position = Vector3.new(math.random(-13.5,13.5),1,math.random(-13.5,13.5))
	cornamount = cornamount + 1
end
while true do
	if cornamount == 5 then
		repeat wait() until cornamount ~= 5 
	elseif cornamount ~= 5 then
		addcorn()
	end
end

Thanks!

1 Like

I would have a folder for all the corn and just check if #Folder:GetChildren() Is bigger or equal to the limit
GetChildren() gives you a list of every child of the folder and the # is to count the numbers of items in a list.
If it is not bigger than spawn in more.
If the corn gets collected just destroy it.

The sign for bigger or equal Is >=

The script you have wont work because you do not substract the value when you delete corn (propably)
Also you do not need to repeat wait until its not equal to 5. Because the corn will be added only when it is not 5

Also to get/set the value of a value object you need to do cornamount.Value and not cornamount.

You’re replacing the cornamount variable with just 0. When you set it to the variable in ReplicatedStorage, you make it so it has a reference to that, but when you say cornamount = 0, you entirely replace it (it doesn’t care about it anymore)

Instead, assuming this is a Value object (NumberValue or IntValue), replace cornamount = 0 with cornamount.Value = 0. Same goes for the other things, just replace cornamount with cornamount.Value.

I fixed the script up, and tried to comment on what I changed

local Services = {ServStorage = game:GetService("ServerStorage"),RepStorage = game:GetService("ReplicatedStorage")} -- Table w/ Services being used

local Corn = Services.ServStorage:WaitForChild("Corn") -- Use :WaitForChild() since things don't always load before the script runs
local CornAmount = Services.RepStorage:WaitForChild("CornAmount")
CornAmount.Value = 0 -- Make sure you're setting its Value

function AddCorn()
	local Corn2 = Corn:Clone()
	Corn2.Position = Vector3.new(math.random(-13.5,13.5),1,math.random(-13.5,13.5)) -- Set properties before parenting because changing properties once inside the game causes microlag
	Corn2.Parent = workspace
	CornAmount.Value = CornAmount.Value + 1
end

-- Don't use while true do loops unless its suppose to specifically stop soon


local WaitTime = 3
local t0 = os.time()+WaitTime -- os.time() gets the current "tick" in seconds and adding 8 makes it equal to 8 seconds in the future
game:GetService("RunService").Heartbeat:Connect(function()
	if os.time() >= t0 then -- Check if it is or is after 8 seconds since the last check
		t0 = os.time()+WaitTime -- Reset it to 8 seconds into the future
		
		if CornAmount.Value ~= 5 then
			AddCorn()
		elseif CornAmount.Value >= 5 then
			task.wait()
		end
	end
end)
1 Like

With these two replies, it worked! Thank you for helping me you guys.

For the people looking for answers, here are the scripts
New scripts:

--ServerScriptService.Script (the generator script)
local SStorage = game:GetService("ServerStorage")
local RepStorage = game:GetService("ReplicatedStorage")
local corn = SStorage:WaitForChild("corn")
local cornamount = RepStorage:WaitForChild("cornamount")
cornamount.Value = 0
local function addcorn()
	wait(8)
	local corn2 = corn:Clone()
	corn2.Parent = game.Workspace
	corn2.Position = Vector3.new(math.random(-13.5,13.5),1,math.random(-13.5,13.5))
	cornamount.Value = cornamount.Value + 1
end
while true do
	if cornamount.Value <= 4 then
		addcorn()
	else
		wait()
	end
end
--ServerStorage.corn.Script (the script that runs when you touch the corn)
local Players = game:GetService("Players")
local corn = script.Parent
local cornamount = game:GetService("StarterGui").cornamount.Frame.Frame.cornamountlabel
local corngrowamount = game:GetService("ReplicatedStorage").cornamount
script.Parent.Touched:Connect(function(otherpart)
	local partparent = otherpart.Parent
	
	local humanoid = partparent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		corn:Destroy()
		corngrowamount.Value = corngrowamount.Value - 1
		local player = Players:GetPlayerFromCharacter(partparent)
		local leaderstats = player.leaderstats
		local cornStat = leaderstats:FindFirstChild("corn")
		if cornStat then
			cornStat.Value = cornStat.Value + 10
		end
	end
end)

(to @maycoleee2231, oops)

2 Likes