Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local Workspace = game:GetService("Workspace")
- local HttpService = game:GetService("HttpService")
- local TweenService = game:GetService("TweenService")
- local Plots = Workspace:WaitForChild("Plots")
- local Player = Players.LocalPlayer
- local function AssignModelUUID(Model: Model)
- local UUID = HttpService:GenerateGUID(false)
- Model:SetAttribute("__UUID", UUID)
- for _, Part in ipairs(Model:GetDescendants()) do
- if Part:IsA("BasePart") then
- Part:SetAttribute("__TreeModelUUID", UUID)
- end
- end
- end
- local function GetStepParts(Model: Model)
- local Steps = {}
- for _, Part in ipairs(Model:GetDescendants()) do
- if Part:IsA("BasePart") and tonumber(Part.Name) then
- if Part:GetAttribute("__TreeModelUUID") == Model:GetAttribute("__UUID") then
- local Idx = tonumber(Part.Name)
- Steps[Idx] = Steps[Idx] or {}
- table.insert(Steps[Idx], Part)
- end
- end
- end
- local Sorted = {}
- for Idx in pairs(Steps) do
- table.insert(Sorted, Idx)
- end
- table.sort(Sorted)
- return Steps, Sorted
- end
- local function HideModelPart(Model: Model)
- for _, Part in Model:GetDescendants() do
- if Part:IsA("BasePart") then
- Part.Transparency = 1
- for _, Text in ipairs(Part:GetDescendants()) do
- if Text:IsA("Texture") or Text:IsA("Decal") or Text:IsA("SurfaceAppearance") then
- if Text:GetAttribute("OriginalTransparency") == nil then
- Text:SetAttribute("OriginalTransparency", Text.Transparency)
- end
- Text.Transparency = 1
- end
- end
- end
- end
- end
- local function AnimateTreeGrowth(Model: Model)
- task.spawn(function()
- local PlantedAt = Model:GetAttribute("PlantedAt")
- local GrowthTime = Model:GetAttribute("GrowthTime")
- if not (PlantedAt and GrowthTime) then return end
- local Elapsed = Workspace:GetServerTimeNow() - PlantedAt
- local Remaining = math.max(GrowthTime - Elapsed, 0)
- local Steps, Sorted = GetStepParts(Model)
- if #Sorted == 0 then return end
- local StepDuration = GrowthTime / #Sorted
- local function RestoreTextures(Part: BasePart)
- for _, Text in ipairs(Part:GetDescendants()) do
- if Text:IsA("Texture") or Text:IsA("Decal") or Text:IsA("SurfaceAppearance") then
- local Orig = Text:GetAttribute("OriginalTransparency")
- if Orig ~= nil then
- Text.Transparency = Orig
- end
- end
- end
- end
- if Elapsed >= GrowthTime then
- for _, Idx in ipairs(Sorted) do
- for _, Part in ipairs(Steps[Idx]) do
- local OrigSize = Part.Size
- local Rot = Part.CFrame - Part.Position
- local BaseY = OrigSize.Y / 2
- local Bottom = Part.Position - Vector3.new(0, BaseY, 0)
- Part.Size = OrigSize
- Part.CFrame = CFrame.new(Bottom + Vector3.new(0, BaseY, 0)) * Rot
- Part.Transparency = 0
- RestoreTextures(Part)
- end
- end
- -- Proximity Prompt
- return
- end
- -- Partial Growth
- for i, Idx in ipairs(Sorted) do
- local StartT = (i - 1) * StepDuration
- local EndT = i * StepDuration
- for _, Part in ipairs(Steps[Idx]) do
- local OrigSize = Part.Size
- local Rot = Part.CFrame - Part.Position
- local BaseY = OrigSize.Y / 2
- local Bottom = Part.Position - Vector3.new(0, BaseY, 0)
- local StartSize = OrigSize / 5
- local HeightOffset = StartSize.Y / 2
- if Elapsed >= EndT then
- Part.Size = OrigSize
- Part.CFrame = CFrame.new(Bottom + Vector3.new(0, BaseY, 0)) * Rot
- Part.Transparency = 0
- RestoreTextures(Part)
- elseif Elapsed >= StartT then
- local Prog = (Elapsed - StartT) / StepDuration
- local TargetSize = StartSize:Lerp(OrigSize, Prog)
- local TargetY = StartSize.Y / 2 + (BaseY - StartSize.Y / 2) * Prog
- Part.Size = TargetSize
- Part.CFrame = CFrame.new(Bottom + Vector3.new(0, TargetY, 0)) * Rot
- Part.Transparency = 0
- RestoreTextures(Part)
- TweenService:Create(Part,
- TweenInfo.new(StepDuration * (1 - Prog), Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
- {
- Size = OrigSize,
- CFrame = CFrame.new(Bottom + Vector3.new(0, BaseY, 0)) * Rot
- }
- ):Play()
- else
- Part.Size = StartSize
- Part.CFrame = CFrame.new(Bottom + Vector3.new(0, HeightOffset, 0)) * Rot
- Part.Transparency = 0
- for _, Text in ipairs(Part:GetDescendants()) do
- if Text:IsA("Texture") or Text:IsA("Decal") or Text:IsA("SurfaceAppearance") then
- Text.Transparency = 1
- end
- end
- local ThisPart = Part
- task.delay(StartT - Elapsed, function()
- ThisPart.Transparency = 0
- RestoreTextures(ThisPart)
- TweenService:Create(ThisPart,
- TweenInfo.new(StepDuration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
- {
- Size = OrigSize,
- CFrame = CFrame.new(Bottom + Vector3.new(0, BaseY, 0)) * Rot
- }
- ):Play()
- end)
- end
- end
- end
- task.delay(Remaining, function()
- -- Proximity Prompt
- end)
- end)
- end
- local function StartAnimationForModel(Model: Model)
- if Model:GetAttribute("__AnimateHooked") then return end
- Model:SetAttribute("__AnimateHooked", true)
- local function RunAnim()
- Model:SetAttribute("Animated", false)
- HideModelPart(Model)
- task.wait(0.5)
- AssignModelUUID(Model)
- AnimateTreeGrowth(Model)
- Model:SetAttribute("Animated", true)
- end
- RunAnim()
- Model:GetAttributeChangedSignal("PlantedAt"):Connect(RunAnim)
- Model:GetAttributeChangedSignal("GrowthTime"):Connect(RunAnim)
- end
- local function ConnectPlantedFolder(Planted: Folder)
- for _, Model in Planted:GetChildren() do
- StartAnimationForModel(Model)
- end
- Planted.ChildAdded:Connect(function(Model: Model)
- task.spawn(function()
- for _ = 1, 50 do
- if Model:GetAttribute("PlantedAt") and Model:GetAttribute("GrowthTime") then
- break
- end
- task.wait(0.1)
- end
- StartAnimationForModel(Model)
- end)
- end)
- end
- local function ConnectPlot(Plot: Model)
- local P = Plot:FindFirstChild("Planted")
- if P then
- ConnectPlantedFolder(P)
- end
- Plot.ChildAdded:Connect(function(Child: Instance)
- if Child:IsA("Folder") and Child.Name == "Planted" then
- ConnectPlantedFolder(Child)
- end
- end)
- end
- for _, Plot in ipairs(Plots:GetChildren()) do
- ConnectPlot(Plot)
- end
- Plots.ChildAdded:Connect(ConnectPlot)
Advertisement
Add Comment
Please, Sign In to add comment