When cloning from Replicated Storage to Workspace (from client) it doesn't work Properly

  1. What do you want to achieve? That it works properly

  2. What is the issue? A part of the model collides with the part it spawns on
    ErrorCar

  3. What solutions have you tried so far? I tried looking but nothing seems to work

Its from a local script in Starter gui

This is not the whole script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local car = script.Parent.Name
local player = players.LocalPlayer

script.Parent.Activated:Connect(function()
	local carRequested = ReplicatedStorage.AllCars[car]:Clone()
	
	if carRequested.Name == car then
		carRequested.Name = player.Name .. "sPreview"
	end
	
	local carExists = game.Workspace:FindFirstChild(player.Name .. "sPreview")
	
	if carExists then
		carExists:Remove()
	end
	
	carRequested.Parent = workspace
	carRequested:MoveTo(Vector3.new(0, 20, 0))
	carRequested:SetPrimaryPartCFrame(CFrame.new(game.Workspace.Showcase.Position) * CFrame.Angles(0, math.rad(45), 0))
end)

Also not everything in the script is my own

Feel free to give feedback on other things but in this script!

1 Like

You can use Model’s :GetExtentsSize() to get a vector3, then apply the y vector to the :MoveTo() function

1 Like

It did this:

	local expand = carRequested:GetExtentsSize()

	carRequested:MoveTo(Vector3.new(0, expand, 0))
	

Is this what you mean?

Kind of, get the y vector of expand, and apply it to the :MoveTo() vector3

How would I do that? (I am not that good of a scripter that cuz i don’t know how to do that)

1 Like
local expand = carRequested:GetExtentsSize()

carRequested:MoveTo(Vector3.new(0, expand.Y, 0))
 -- Vector3.new(0, expand, 0) > Vector3.new(0, expand.Y, 0)

What do I need to do with this?

Sorry for late response but, that was a comment comparing your previous attempt (doesn’t affect the code so you can delete it)

I fixed it!. I set the position in numbers in stead of finding the position of a Part in the script!

Not quite, :GetExtentsSize gets the smallest possible size that all the parts in a model can fit within aligned with the orientation of its PrimaryPart. Doing what you’re doing, you’re assuming that the PrimaryPart is in the center of the model, which it isn’t necessarily, you also forgot to divide by 2.

What I’d do is calculate the distance between the ground and the model’s PrimaryPart by using :GetBoundingBox like so to get the model’s size as well as a fully centered position that we can get from the returned CFrame value:

function GetGroundDistance(model)
	local cf, size = model:GetBoundingBox()
	-- getting the bottom position of the model (think of this as the ground)
	local bottomCFrame = cf * CFrame.new(0, -size.Y/2, 0)
	-- getting the offset between the bottom and the model's PrimaryPart
	local objectSpace = bottomCFrame:ToObjectSpace(model.PrimaryPart.CFrame)
	-- return the height that the model needs to be offset when repositioned
	return objectSpace.Y
end

How to use

local spawnPart = workspace.Baseplate
local targetPosition = spawnPart.Position + Vector3.new(0, spawnPart.Size.Y/2, 0)
someModel:SetPrimaryPartCFrame(CFrame.new(targetPosition + Vector3.new(0, GetGroundDistance(someModel), 0)))
2 Likes

Its returns this error: attempt to index nil with ‘ToObjectSpace’

This is how I did it in the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local spawnPart = workspace.Baseplate

local car = script.Parent.Name
local player = players.LocalPlayer

function GetGroundDistance(model)
	local cf, size = model:GetBoundingBox()
	-- getting the bottom position of the model (think of this as the ground)
	local bottomCFrame = cf * CFrame.new(0, -size.Y/2, 0)
	-- getting the offset between the bottom and the model's PrimaryPart
	local objectSpace = bottomPoint:ToObjectSpace(model.PrimaryPart)
	-- return the height that the model needs to be offset when repositioned
	return objectSpace.Y
end

script.Parent.Activated:Connect(function()
	local carRequested = ReplicatedStorage.AllCars[car]:Clone()
	
	if carRequested.Name == car then
		carRequested.Name = player.Name .. "sPreview"
	end
	
	local carExists = game.Workspace:FindFirstChild(player.Name .. "sPreview")
	
	if carExists then
		carExists:Remove()
	end
	carRequested.Parent = workspace
	
	local targetPosition = spawnPart.Position + Vector3.new(0, spawnPart.Size.Y/2, 0)
	carRequested:SetPrimaryPartCFrame(CFrame.new(targetPosition + Vector3.new(0, GetGroundDistance(carRequested), 0)))
end)

Have a look at the updated code, just a few oversights from my part