Cakey3101

GAG Plant Service 2

Aug 9th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | Source Code | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local HttpService = game:GetService("HttpService")
  3. local Workspace = game:GetService("Workspace")
  4. local ServerScriptService = game:GetService("ServerScriptService")
  5.  
  6. local PlantConfig = require(ReplicatedStorage.Configs.PlantConfig)
  7.  
  8. local MIN_SCALE, MAX_SCALE = 1, 1.8
  9.  
  10. local PlantService = {}
  11.  
  12. local function RollForHeight()
  13.     local T = math.random()
  14.    
  15.     return MIN_SCALE + (MAX_SCALE - MIN_SCALE) * (T * T)
  16. end
  17.  
  18. local function PlaceModelOnGround(Model: Model, Position: Vector3)
  19.     Model.PrimaryPart = Model.PrimaryPart or Model:FindFirstChildOfClass("BasePart")
  20.     if not Model.PrimaryPart then return end
  21.    
  22.     local YSize = Model.PrimaryPart.Size.Y
  23.    
  24.     local Params = RaycastParams.new()
  25.     Params.FilterType = Enum.RaycastFilterType.Exclude
  26.     Params.FilterDescendantsInstances = { Model, Workspace.Plots }
  27.    
  28.     local Origin = Vector3.new(Position.X, Position.Y + 500, Position.Z)
  29.     local Direction = Vector3.new(0, -500, 0)
  30.     local Result = Workspace:Raycast(Origin, Direction, Params)
  31.     local GroundY = Result and Result.Position.Y or Position.Y
  32.    
  33.     Model:SetPrimaryPartCFrame(CFrame.new(Position.X, GroundY + YSize / 2, Position.Z))
  34. end
  35.  
  36. local function ApplyHeightToModel(Model: Model, Scale: number)
  37.     for _, Part in ipairs(Model:GetDescendants()) do
  38.         if Part:IsA("BasePart") then
  39.             local OrigSize = Part.Size
  40.             local Rot = Part.CFrame - Part.CFrame.Position
  41.             local NewSize = OrigSize * Scale
  42.             Part.Size = NewSize
  43.             local BottomPos = Part.CFrame.Position - Vector3.new(0, OrigSize.Y / 2, 0)
  44.             Part.CFrame = CFrame.new(BottomPos + Vector3.new(0, NewSize.Y / 2, 0)) * Rot
  45.         end
  46.     end
  47. end
  48.  
  49. function PlantService.SpawnTreeModel(Player: Player, SeedType: string, Position: Vector3, PlantedFolder: Folder, Modifier, PlantdAt: number, HeightScale: number, TreeID: number)
  50.     HeightScale = HeightScale -- Or Roll
  51.     local Template = ReplicatedStorage.Trees:FindFirstChild(SeedType)
  52.     if not Template then return end
  53.    
  54.    
  55.     local Model = Template:Clone()
  56.     PlaceModelOnGround(Model, Position)
  57.     ApplyHeightToModel(Model, HeightScale)
  58.    
  59.    
  60.     Model:SetAttribute("HeightScale", HeightScale)
  61.     Model:SetAttribute("SeedType", SeedType)
  62.     Model:SetAttribute("PlantedAt", PlantdAt)
  63.    
  64.     local Yaw = math.rad(math.random(0, 360))
  65.     if Model.PrimaryPart then
  66.         local CF = Model.PrimaryPart.CFrame
  67.         Model:SetPrimaryPartCFrame(CFrame.new(CF.Position) * CFrame.Angles(0, Yaw, 0))
  68.     end
  69.    
  70.     for _, Part in ipairs(Model:GetDescendants()) do
  71.         if Part:IsA("BasePart") and Part.Name:match("^%d+$") then
  72.             Part.Anchored = true
  73.             Part.CanCollide = true
  74.         end
  75.     end
  76.    
  77.     if Modifier then
  78.         -- Episode 4 (Add Modifier)
  79.     end
  80.    
  81.     local BaseTime = PlantConfig[SeedType].GrowthTime
  82.    
  83.     Model:SetAttribute("GrowthTime", BaseTime)
  84.     Model:SetAttribute("PlotName", PlantedFolder.Parent and PlantedFolder.Parent.Name or nil)
  85.     Model:SetAttribute("TreeID", TreeID)
  86.    
  87.     Model.Parent = PlantedFolder
  88.    
  89.     return Model
  90. end
  91.  
  92. function PlantService.PlantSeed(Player: Player, PlotName: string, SeedType: string, Position: Vector3, Tool: Tool, LocalOffset: Vector3)
  93.     local Config = PlantConfig[SeedType]
  94.     if not Config then return end
  95.    
  96.     local TreeId = HttpService:GenerateGUID(false)
  97.     local Now = Workspace:GetServerTimeNow()
  98.     local Modifier = nil -- Episode 4
  99.     local HeightScale = RollForHeight()
  100.    
  101.     local Plot = Workspace.Plots:FindFirstChild(PlotName)
  102.     if not Plot then return end
  103.    
  104.     local Folder = Plot:FindFirstChild("Planted") or Instance.new("Folder", Plot)
  105.     Folder.Name = "Planted"
  106.    
  107.     local Tree = PlantService.SpawnTreeModel(Player, SeedType, Position, Folder, Modifier, Now, HeightScale, TreeId)
  108.     if not Tree then return end
  109.    
  110.     -- Data
  111.    
  112.     warn(SeedType, "Planted")
  113. end
  114.  
  115. return PlantService
Advertisement
Add Comment
Please, Sign In to add comment