-
What do you want to achieve? I want all of the cloned models rotation to be correct.
-
What is the issue? I’m trying to make a script that generates trees, stones and bushes across the map randomly. The problem is with the object’s orientation. When they’re cloned and their pivot is set to a raycast result’s position, it’s not rotated correctly as it’s supposed to be. This happens to some objects, but doesn’t happen to the rest of the objects…
The objects are all located in ServerStorage in a folder and in the folders in that folder.
-
What solutions have you tried so far? So far I’ve tried changing the pivot rotation with :GetPivot().Orientation (or rotation) to the same object but the uncloned version’s rotation (just thought it would work lmao)
I did not find any solutions on the DevForum either.
local ObjectsFolder = game.ServerStorage:WaitForChild("Objects")
local CollectionService = game:GetService("CollectionService")
function GenerateObjects()
for i = 1, 400, 1 do
wait()
for _, folder in (ObjectsFolder:GetChildren()) do
for _, object in (folder:GetChildren()) do
CollectionService:AddTag(object, "Objects")
local RandomXValue = math.random(-2715.887, 2161.695)
local RandomZValue = math.random(-2689.518, 1726.377)
local Origin = Vector3.new(RandomXValue, 32.715, RandomZValue)
local RayDirection = Vector3.new(0,-500, 0)
local params = RaycastParams.new()
params.IgnoreWater = false
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = {workspace.Terrain}
local raycastResult = workspace:Raycast(Origin, RayDirection, params)
if raycastResult then
if raycastResult.Instance:IsA("Terrain") then
if raycastResult.Material == Enum.Material.Water then continue end
if raycastResult.Material == Enum.Material.Ice then continue end
if raycastResult.Material == Enum.Material.Snow then continue end
if raycastResult.Material == Enum.Material.Sand and object:GetAttribute("Type") == "Cactus" then
local ObjectClone = object:Clone()
local Orientation, ModelSize = ObjectClone:GetBoundingBox()
ObjectClone.Parent = workspace
ObjectClone:PivotTo(CFrame.new(raycastResult.Position + Vector3.new(0, ModelSize.Y/2, 0)))
elseif raycastResult.Material ~= Enum.Material.Sand and object:GetAttribute("Type") ~= "Cactus" then
local ObjectClone = object:Clone()
local Orientation, ModelSize = ObjectClone:GetBoundingBox()
ObjectClone.Parent = workspace
ObjectClone:PivotTo(CFrame.new(raycastResult.Position + Vector3.new(0,ModelSize.Y/2,0)))
end
end
end
end
end
end
end
GenerateObjects()
while task.wait(40*60) do
GenerateObjects()
end