Hello, I am currently stuck on why cloned models don’t stay in the terrain but stay in parts. I want the trees to stay in the ground rather than being above it. Both the terrain instance and the part instance (not the trees) have the same CollisionGroup, default.
How can I fix this so I don’t have random trees floating above terrain. I have trees spawn in and respawn because I want players to chop them down.
External MediaI doubt the code is the issue but just in case, I will show the code:
Server script:
local SS = game:GetService("ServerStorage")
local SSS = game:GetService("ServerScriptService")
local treesFolder = SS.Trees
local treeModuleScript = require(SSS.moduleScripts.treesModuleScript)
local treesRespawnTime = SSS.moduleScripts.treesModuleScript.respawnTime.Value
local worldTreesFolder = script.Parent
task.wait(1)
for i, v in pairs(worldTreesFolder:GetDescendants()) do
if v:IsA("BoolValue") and v.Name == "isChopped" then
local treeSpawner = v.Parent:FindFirstChild("spawner")
local isChopped = v
treeModuleScript.randomizeTree(treeSpawner)
treeModuleScript.changeTree(treeSpawner)
isChopped.Changed:Connect(function()
if isChopped.Value == true then
task.wait(treesRespawnTime)
treeModuleScript.randomizeTree(treeSpawner)
treeModuleScript.changeTree(treeSpawner)
isChopped.Value = false
end
end)
end
end
Module script:
local treesModule = {}
local TS = game:GetService("TweenService")
local SS = game:GetService("ServerStorage")
local treesFolder = SS.Trees
local respawnTime = script.respawnTime.Value
local tweenInfo = TweenInfo.new(
20,
Enum.EasingStyle.Quad,
Enum.EasingDirection.In
)
local worldConfig = workspace.worldConfig
local worldTreesFolder = workspace.choppableTrees
local treeModel = treesFolder.PineTree
local baseStats = {
["Coins"] = 2,
["Health"] = 2
}
local normalColors = {
Color3.fromRGB(127, 142, 100), -- light
Color3.fromRGB(91, 154, 76), -- normal
Color3.fromRGB(44, 101, 29), -- dark
Color3.fromRGB(39, 70, 45) -- darker
}
local fallColors = {
Color3.fromRGB(205, 100, 25), -- Orange
Color3.fromRGB(230, 140, 50), -- Gold
Color3.fromRGB(240, 180, 20), -- Yellow
Color3.fromRGB(195, 25, 25), -- Red
Color3.fromRGB(150, 0, 0), -- Crimson
Color3.fromRGB(190, 0, 50), -- Purple Red
Color3.fromRGB(95, 165, 85), -- Light Green
Color3.fromRGB(65, 130, 65) -- Green
}
function treesModule.randomizeTree(spawnModel)
local treeClone = treeModel:Clone()
local config = treeClone.treeStats
local randomSize = math.random(3, 14) / math.random(1, 2)
local coinsMult = math.round((baseStats["Coins"]*randomSize)/0.95)
local healthMult = math.round((baseStats["Health"]*randomSize)/1.65)
config:SetAttribute("sizeScale", randomSize)
config:SetAttribute("Coins", coinsMult)
config:SetAttribute("Health", healthMult)
local multStats = {
["Size"] = randomSize,
["Coins"] = coinsMult,
["Health"] = healthMult
}
treeClone.Parent = spawnModel
treeClone:MoveTo(spawnModel.Position)
end
function treesModule.changeTree(spawnModel)
local treeSpawner = spawnModel
local isChopped = spawnModel.Parent.isChopped
local treeClone = spawnModel:WaitForChild("PineTree")
local config = treeClone.treeStats
local randomChance1 = math.random(1, #normalColors)
local randomColorNormal = normalColors[randomChance1]
local randomChance2 = math.random(1, #fallColors)
local randomColorFall = fallColors[randomChance2]
for i, v in pairs(treeClone:GetChildren()) do
if v.Name ~= "Leaves" and (v:IsA("MeshPart") or v:IsA("Part")) then
v.CollisionGroup = "trees"
end
if v.Name ~= "Snow" and (v:IsA("MeshPart") or v:IsA("Part")) then
v.Transparency = 0
v.Anchored = true
end
if v.Name == "Leaves" and v:IsA("MeshPart") then
v.CollisionGroup = "leaves"
v.CanCollide = true
if worldConfig:GetAttribute("season") == "fall" then
v.Color = randomColorFall
else
v.Color = randomColorNormal
end
end
if v.Name == "Snow" and v:IsA("MeshPart") then
v.CollisionGroup = "leaves"
if worldConfig:GetAttribute("season") == "winter" then
TS:Create(v, tweenInfo, {Transparency = 0}):Play()
else
TS:Create(v, tweenInfo, {Transparency = 1}):Play()
end
end
end
treeClone:ScaleTo(config:GetAttribute("sizeScale"))
end
return treesModule