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