Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local HttpService = game:GetService("HttpService")
- local Workspace = game:GetService("Workspace")
- local ServerScriptService = game:GetService("ServerScriptService")
- local PlantConfig = require(ReplicatedStorage.Configs.PlantConfig)
- local MIN_SCALE, MAX_SCALE = 1, 1.8
- local PlantService = {}
- local function RollForHeight()
- local T = math.random()
- return MIN_SCALE + (MAX_SCALE - MIN_SCALE) * (T * T)
- end
- local function PlaceModelOnGround(Model: Model, Position: Vector3)
- Model.PrimaryPart = Model.PrimaryPart or Model:FindFirstChildOfClass("BasePart")
- if not Model.PrimaryPart then return end
- local YSize = Model.PrimaryPart.Size.Y
- local Params = RaycastParams.new()
- Params.FilterType = Enum.RaycastFilterType.Exclude
- Params.FilterDescendantsInstances = { Model, Workspace.Plots }
- local Origin = Vector3.new(Position.X, Position.Y + 500, Position.Z)
- local Direction = Vector3.new(0, -500, 0)
- local Result = Workspace:Raycast(Origin, Direction, Params)
- local GroundY = Result and Result.Position.Y or Position.Y
- Model:SetPrimaryPartCFrame(CFrame.new(Position.X, GroundY + YSize / 2, Position.Z))
- end
- local function ApplyHeightToModel(Model: Model, Scale: number)
- for _, Part in ipairs(Model:GetDescendants()) do
- if Part:IsA("BasePart") then
- local OrigSize = Part.Size
- local Rot = Part.CFrame - Part.CFrame.Position
- local NewSize = OrigSize * Scale
- Part.Size = NewSize
- local BottomPos = Part.CFrame.Position - Vector3.new(0, OrigSize.Y / 2, 0)
- Part.CFrame = CFrame.new(BottomPos + Vector3.new(0, NewSize.Y / 2, 0)) * Rot
- end
- end
- end
- function PlantService.SpawnTreeModel(Player: Player, SeedType: string, Position: Vector3, PlantedFolder: Folder, Modifier, PlantdAt: number, HeightScale: number, TreeID: number)
- HeightScale = HeightScale -- Or Roll
- local Template = ReplicatedStorage.Trees:FindFirstChild(SeedType)
- if not Template then return end
- local Model = Template:Clone()
- PlaceModelOnGround(Model, Position)
- ApplyHeightToModel(Model, HeightScale)
- Model:SetAttribute("HeightScale", HeightScale)
- Model:SetAttribute("SeedType", SeedType)
- Model:SetAttribute("PlantedAt", PlantdAt)
- local Yaw = math.rad(math.random(0, 360))
- if Model.PrimaryPart then
- local CF = Model.PrimaryPart.CFrame
- Model:SetPrimaryPartCFrame(CFrame.new(CF.Position) * CFrame.Angles(0, Yaw, 0))
- end
- for _, Part in ipairs(Model:GetDescendants()) do
- if Part:IsA("BasePart") and Part.Name:match("^%d+$") then
- Part.Anchored = true
- Part.CanCollide = true
- end
- end
- if Modifier then
- -- Episode 4 (Add Modifier)
- end
- local BaseTime = PlantConfig[SeedType].GrowthTime
- Model:SetAttribute("GrowthTime", BaseTime)
- Model:SetAttribute("PlotName", PlantedFolder.Parent and PlantedFolder.Parent.Name or nil)
- Model:SetAttribute("TreeID", TreeID)
- Model.Parent = PlantedFolder
- return Model
- end
- function PlantService.PlantSeed(Player: Player, PlotName: string, SeedType: string, Position: Vector3, Tool: Tool, LocalOffset: Vector3)
- local Config = PlantConfig[SeedType]
- if not Config then return end
- local TreeId = HttpService:GenerateGUID(false)
- local Now = Workspace:GetServerTimeNow()
- local Modifier = nil -- Episode 4
- local HeightScale = RollForHeight()
- local Plot = Workspace.Plots:FindFirstChild(PlotName)
- if not Plot then return end
- local Folder = Plot:FindFirstChild("Planted") or Instance.new("Folder", Plot)
- Folder.Name = "Planted"
- local Tree = PlantService.SpawnTreeModel(Player, SeedType, Position, Folder, Modifier, Now, HeightScale, TreeId)
- if not Tree then return end
- -- Data
- warn(SeedType, "Planted")
- end
- return PlantService
Advertisement
Add Comment
Please, Sign In to add comment