How do you clone models to a position?

I have a ModuleScript in workspace that will clone a model of a note for a rhythm game. My ModuleScript is called with require() in a LocalScript in StarterPlayerScripts. The script below will actually clone the object just fine, but it won’t move it to the position I want it to go.

function note(_time, track)
    if track > 4 or track < 1 then
	    warn(warningMessageInvalidTrack)
    end

    if _time > math.floor((workspace.MUSIC.TimeLength)*1000) then
	    warn(warningMessageInvalidTime)
    end

    repeat
	    wait(0)
    until workspace.songPosition.Value > _time --Waits until the song position is greater than a certain value

    local original = workspace.InGameObjects.Note --19 thru 21 copies the part and parents it
    local copy = original:Clone()	
    copy.Parent = original.Parent

    local t = 0     --"t" will reperesent the X coordinate for the corresponding track
    if track == 1 then
	    t = -8.68
    end

    if track == 2 then
	    t = -1.68
    end

    if track == 3 then
	    t = 5.32
    end

    if track == 4 then
	    t = 12.32
    end	
    local y_pos = 0.133
    local z_pos = -9.435

    copy.Position = Vector3.new(t,y_pos,z_pos) -- y_pos and z_pos are constants, meaning they don't "adapt" to the track number.

end

2 Likes

CFraming is more efficient.
copy.CFrame = CFrame.new(t,y_pos,z_pos)

…but if it’s a model, use m:SetPrimaryPartCFrame(CFrame.new(t, y_pos, z_pos))

Also simplifying:

if track == 1 then
   t = -8.68
elseif track == 2 then
   t = -1.68
elseif track == 3 then
   t = 5.32
elseif track == 4 then
   t = 12.32
end
8 Likes

To move an entire model you need to use model:SetPrimaryPartCFrame(). This method will move the model to the given CFrame based on the model’s PrimaryPart.

copy:SetPrimaryPartCFrame(CFrame.new(t, y_pos, z_pos))
Make sure that you’ve set the model’s PrimaryPart before doing this though.

13 Likes

This did the trick! Thanks!

2 Likes

Yeah, I was actually trying that, but I think I spelled SetPrimaryPartCFrame wrong AND I didn’t use the “:”; I used a “.”. Thanks for the lightning quick response. :sunglasses:

2 Likes

Can you post a link to your game?
It sounds cool!

3 Likes