PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

SetPrimaryPartCFrame

Sets the BasePart/CFrame of the Model's Model/PrimaryPart. All other parts in the model will also be moved and will maintain their orientation and offset respective to the Model/PrimaryPart.

Note, this function will throw an error if no Model/PrimaryPart exists for the model. This can cause issues if, for example, the primary part was never set or has been destroyed. Therefore, it is recommended the developer check the Model/PrimaryPart exists before using this function. For example:

if model.PrimaryPart then
	model:SetPrimaryPartCFrame(cf)
end

A common use for this is for the ‘teleportation’ of player characters to different positions.

Parameters

Name Type Default Description

The DataType/CFrame to be set.

Returns

Return Type Summary

Code Samples


Character SetPrimaryPartCFrame

In this sample every Player character Model added will be moved to 0, 500, 0 three seconds after spawning.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(newPlayer)
	newPlayer.CharacterAdded:Connect(function(characterModel)
		wait(3)
		characterModel:SetPrimaryPartCFrame(CFrame.new(0, 500, 0))
	end)
end)

SetPrimaryPartCFrame

In this code sample a Model is created with a simple arrangement of Parts. The PrimaryPart of this Model is then set, meaning Model:SetPrimaryPartCFrame can then be used.

To demonstrate SetPrimaryPartCFrame, a simple loop is then run which switches the model between two CFrames every second.

-- define two CFrames
local startCFrame = CFrame.new(0, 10, 0)
local endCFrame = CFrame.new(0, 15, 0) * CFrame.Angles(0, math.rad(90), 0)

-- create a simple model
local model = Instance.new("Model", game.Workspace)

local part = Instance.new("Part", model)
part.Size = Vector3.new(5, 1, 5)
part.Anchored = true
part.CFrame = startCFrame
part.BrickColor = BrickColor.new("Bright green")

local part2 = Instance.new("Part", model)
part2.Size = Vector3.new(5, 1, 1)
part2.Anchored = true
part2.CFrame = startCFrame * CFrame.new(0, 1, 0) * CFrame.Angles(0, math.rad(90), 0)
part2.BrickColor = BrickColor.new("Bright red")

-- set the model's PrimaryPart
model.PrimaryPart = part 

-- loop between the two CFrames
while true do
	wait(1)
	model:SetPrimaryPartCFrame(startCFrame)
	wait(1)
	model:SetPrimaryPartCFrame(endCFrame)
end