Co-Routine or just one big long Tween

Hi everyone. Finally my first post. I’ve just recently started learning Roblox Lua (Coming from Unity) so I don’t know all possible avenues to accomplish certain things and still aren’t sure performance wise how some techniques may lag a game. I’m making a game as I learn. So here’s my first question. Sorry in advance if this is long winded :face_with_open_eyes_and_hand_over_mouth:

In this game I have 24 plants that will only grow if the plot they are in is marked as watered. When its watered, each plant will grow by starting a co-routine to grow it over time. Not a lot of time. Perhaps maximum a couple of minutes to fully grow it. The plant has 5 growth stages until ready to pick. if the ‘watered’ attribute changes to false then they stop growing.

The script so far works no problem, but my question is, performance wise could I just have the plant Tween its size in one big long Tween. Only reason I’m asking is because it would be much simpler with a big tween per plant over 1 or 2 minutes and also I could pause the tween if the plot becomes un-watered and resume it when watered again. Then at the end of the tween it would be fully grown. It may well be that neither of these ways im doing it are very good so just wanted your thoughts.

Here’s my working code so far. Keep in mind its not finished (I will move the tweening process to the Client later and use a remote, also when the plant is harvested it needs to reset and start growing again).
Just want to know if Im on the right track…

This script is on every plant along with a Proximity Prompt

local Garden = script.Parent.Parent.Parent
local Module = require(Garden.Crops.ModuleScript)
local Crop = script.Parent

local GrowPlants = function() -- Used with Co-Routine to Grow the plant
	for i = 0,5,1 do
		task.wait(math.random(Module.MinGrowTime,Module.MaxGrowtime))
		Crop:SetAttribute("GrowthStage",i)
		task.wait(.5)
		Module.Grow(Crop,i)
	end
	Crop:SetAttribute("GrowthStage",5)
	task.wait(.5)
	Module.Grow(Crop,5)
	task.wait(5)
	Crop.ProximityPrompt.Enabled = true
end

-- Start or Stop Co-Routine Depending on Attribute
local Thread = nil
Garden.AttributeChanged:Connect(function()
	if Garden:GetAttribute("watered") then
		Thread = coroutine.create(GrowPlants)
		coroutine.resume(Thread)
	else
		coroutine.close(Thread)
	end
end)

and this Module they all use

local module = {}
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(2)
local YPOS = {11.2,11.5,11.8,12.2,12.5} -- Table for Y Position depending on Plant Age

module.MinGrowTime = 10 -- Minimum time for Growth
module.MaxGrowtime = 30 -- Maximum time for Growth

function module.Grow(crop,age)
	if age < 5  then
		local position = Vector3.new(crop.Position.X,YPOS[age+1],crop.Position.Z) -- Assign Y Position because plant gets bigger and unroots itself
		local goal = {["Position"]= position} -- Set Goal for Move Tween
		local size = Vector3.new(age+1,age+1,age+1) -- Set Size for new growth of plant
		local sizegoal = {["Size"] = size} -- Set Goal for Size Tween
		TweenService:Create(crop,tweenInfo,goal):Play()
		TweenService:Create(crop,tweenInfo,sizegoal):Play()
	elseif age == 5 then
		-- Ripen plant ready to be picked
		TweenService:Create(crop,TweenInfo.new(10),{Color = Color3.fromRGB(255, 237, 101)}):Play()
	end
end
return module
1 Like

Howdy!

Welcome to the forum. I saw your post three days ago but didn’t have enough time to answer then. I’m surprised you didn’t get any replies in the meantime, I hope you are not discouraged because it got pushed back a bit.

Tweens

While the implementation detail are not public, it’s clear that tweened property updates are visible per frame. I’m pretty sure the performance difference between one long tween and multiple sequential tweens is minuscule.

As you are already planning, the visuals will be better on the client side. It’s not that server tweens are less frequest, but the physics send rate doesn’t match the refresh rate.

Side note: a single tween can update multiple properties.

Now to the main question.

Are tweens suitable in this case?

Tweens can cause lag in large numbers. How large obviously depends on the device, as average computers can probably run a couple of thousand tweens simultaneously before reaching frame drops in a purely default, empty baseplate.

You said you have 24 independent plants. They can be smoothly tweened just fine with no problems. But depending on the game, you might want to consider whether tweens are really necessary (or necessary at all times). If you had a larger farm with 100 plants, it would be better to not update as rapidly, or only update once in each growth stage like Farming and Friends does.

Does the smooth growth have to be simulated at all times?

I don’t know what type of game your are making, but this is something to be considered too. Can the player ever leave the garden? Is growing it just one of the many objectives in the game? Do other players have their own gardens? Do those gardens have to be tweened too?

Do the tweens have to run at all times? One optimisation tactic would be to keep track of the growth time separately and only smoothly grow plants while the player is close.

Server-client communication

The server is the authority and the client does the growth. One way to let the client know about stages and transitions is via remotes, though listening to attribute changes sounds easier - client responding to attribute changes on server.

Managing a collection

Another optimisation is handling all these plants from one script, perhaps using CollectionService, traditional folder… your choice. Treat each plant individually as you do now, but from one place in a loop.


You are definitely on a good track and I hope you find this helpful. Feel free to message me here or in private messages for details or any questions.

2 Likes

Hey there and thanks for the reply. And now I just realized I got a reply :slight_smile:
I’ve been doing some more work on it and decided to stick with the 5 stages and only have them tween at each stage. Also great idea about only doing the tweening when someone is close by and also after some thought maybe a tween isn’t really necessary. They could just instantly change size as I’ve seem other games do. Because the bigger picture is that these plants are only grown as food to feed animals that will be in the game.

What I am interested in though is the Collection Service. Ive used that thus far just to keep count of the amount of wool that is dropped by my sheep using a Tag. But I’m sure there is a lot more I could do with it. Currently each plant has its own script which I hate because in the future if I need to change something in the script then I have to change them all. I did try using a loop but couldn’t quite get my head around it. I tried to rename all the plants to unique names and then create references to all of them in one script but when it came time for the listener in the script for the watered attribute I got frustrated. But it sounds totally doable and probably way more efficient and tidy so Ill attempt it again :laughing:

I’m actually in the process of rewriting the whole game because I have so many scripts its getting ridiculous to follow my own game structure. So I’m creating Modules to get it streamlined. And also moving Tweens to the client side.

I’m also developing a program that allows programmers to flowchart their game code in graphical form to make it easier for programmers to track what in the heck their game is doing. I couldn’t find anything on the web so thought ill just make one.