Hey!
Alright, so I want to clone an object from replicatedstorage (Let's call it car) and place it into a model (Lets call it road) in workspace explorer. Then in the game I want to move this car onto a pad which is part of road. I have tried car:MoveTo(Vector3.new(pad.Position))
(Since it is a part) but it does not work, and instead the car spawns somewhere well away from the pad. Any help?
I believe you mean that it will move the model above where you want it, because ROBLOX tends to do that. There are a few ways to do this. If you haven't already, read up on the CFrame wiki page. This will be invaluable in the script.
The first thing we'll need to do is use the method "GetModelCFrame". This gets the CFrame for the model. We'll set "modelCF" to it.
modelCF=car:GetModelCFrame()
Next, we have to figure out where we want to move it to. It should be a CFrame. We'll set "targetCFrame" to "pad.CFrame".
targetCFrame=pad.CFrame
Now we have to move the parts. The way you do this will vary depending on the model structure; this is the simple way. If you have submodels with parts in them, this method won't work, and you'll need recursion. We'll get the relative CFrame from modelCF to the part's CFrame, and then use that relation to move it to targetCFrame.
for i,v in pairs(car:children()) do if v:IsA("BasePart") then v.CFrame=targetCFrame:toWorldSpace(modelCF:toObjectSpace(v.CFrame)) end end
This should complete the move. You can, of course, define a more general function:
function moveModel(model,targetCFrame) for i,v in pairs(car:children()) do if v:IsA("BasePart") then v.CFrame=targetCFrame:toWorldSpace(model:GetModelCFrame():toObjectSpace(v.CFrame)) end end end moveModel(car,pad.CFrame)
You could also use recursion to get the parts inside of submodels, as mentioned before.
pad.Position
is already a Vector3 object, you don't need to call Vector3.new
on it.
However, since positions are center-based, you might want to add some legroom onto that Y axis.
car:MoveTo(pad.Position + Vector3.new(0, 6, 0))