Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to change a CFrame of something without changing the orientation?

So... I have a game where well

u fly on a train to the glitchlands

u've probably heard of this before

anyways, current activation script here

local p = script.Parent -- InitialBase
local ding = workspace.getonguys -- sound
local choochoo = workspace.choochoo -- sound
local model = script.Parent.Parent -- workspace.Train, the model containing the InitialBase
local vroom = workspace.train -- sound
local db = true -- debounce
local _S = require(workspace.VariableThingies.Services) -- grab some variabulz
local thing = p.Touched:Connect(function(otherPart) -- connect to ze touched function
	if db then -- deboing
	db = false
	ding:Play() -- ding!
	ding.Ended:Wait() --  tick tock
	wait() -- arf
	for i, p in pairs(game:GetService("Players"):GetPlayers()) do
		local char = p.Character
		local humroot = char.HumanoidRootPart
		if humroot.Position.Y < 8.83 then -- if they're not on the train then 
			humroot.CFrame = CFrame.new(math.random(-2.75, 5), 8.86, math.random(-20, 9)) -- teleport them to a random spot on the train
		end
		local bodyForce = Instance.new("BodyForce") -- this is to help prevent the player from bugging out, you'll see what from later
		bodyForce.Force = Vector3.new(0, -50, 0)
		bodyForce.Parent = humroot
		char.Humanoid.Jumping:Connect(function() -- this keeps the player's Y velocity constant when they jump
			local Vel = humroot.Velocity
			bodyForce.Force = Vector3.new()
			_S.RunService.Heartbeat:Wait()
			repeat
				humroot.Velocity = Vector3.new(humroot.Velocity.X, humroot.Velocity.Y, Vel.Z)
				_S.RunService.Heartbeat:Wait()
			until char.Humanoid:GetState() == Enum.HumanoidStateType.Landed
			bodyForce.Force = Vector3.new(0, -50, 0)
		end)
	end
	model.Barriers.Door.CanCollide = true
	model.Barriers.Door.DoorScript.Disabled = false
	for i, p in pairs(model:GetDescendants()) do
		if p:IsA("BasePart") then
			local Vel = Instance.new("BodyVelocity")
			Vel.Velocity = Vector3.new(0, 0, 0) -- keep the train up
			Vel.Name = "Velocity"
			Vel.MaxForce = Vector3.new(4000, math.huge, math.huge) -- big fat load needs a big fat force
			Vel.Parent = p
			_S.CollectionService:AddTag(Vel, "VelocityObject")
		end
	end
	script.Parent.HoldmeWeld:Destroy() -- destroy the weld to the ramp
	model.Ramp:Destroy() -- ayyyyyyyyy destroy da ramp
	workspace.SpawnLocation.Enabled = false
	model.TrainSpawn.Enabled = true
	wait(3)
	choochoo:Play() -- CHOO CHOO
	choochoo.Ended:Wait() -- tick tock
	wait(1)
	vroom:Play() -- i like trains
	wait(1.65)
	for i,v in pairs(_S.CollectionService:GetTagged("VelocityObject")) do
		v.Velocity = Vector3.new(0, 0, -500) -- told you you'd figure out why
	end
	model.Decor.InfoStand.TextPart.update.Disabled = false
	for i,plr in pairs(_S.Players:GetPlayers()) do
		local humroot = plr.Character.HumanoidRootPart
		humroot.Velocity = Vector3.new(humroot.Velocity.X, humroot.Velocity.Y, -500 + humroot.Velocity.Z) -- ayy u ain't flyin' off now brubber
	end
	end
end)

But the system is unstable, because the velocity causes players to fall off very easily, despite the invisible barriers.

To fix this, I want to replace

for i,v in pairs(_S.CollectionService:GetTagged("VelocityObject")) do
	v.Velocity = Vector3.new(0, 0, -500)
end

with something like

local MoveTrain = coroutine.create(function()
	while true do
		for i, p in pairs(model:GetDescendants()) do if p:IsA("BasePart") then
			p.CFrame = CFrame.new(p.CFrame.X, p.CFrame.Y, p.CFrame.Z - 50)
		end end
	wait(0.1)
	end
end)
coroutine.resume(MoveTrain)

and

for i,plr in pairs(_S.Players:GetPlayers()) do
	local humroot = plr.Character.HumanoidRootPart
	humroot.Velocity = Vector3.new(humroot.Velocity.X, humroot.Velocity.Y, -500 + humroot.Velocity.Z)
end

with

for i,plr in pairs(_S.Players:GetPlayers()) do
	local humroot = plr.Character.HumanoidRootPart
	local co = coroutine.create(function()
		while true do
			humroot.CFrame = CFrame.new(humroot.CFrame.X, humroot.CFrame.Y, humroot.CFrame.Z - 50)
			wait(0.1)
		end
	end)
	coroutine.resume(co)
end

But issue:

#Setting CFrame resets the orientation of a part to 0, 0, 0

And it's not like Roblox is gonna be nice with mah attempts, using humroot.Orientation will just throw the player into the astral dimension (#doctorstrangereference #whyamiusinghashtags #hashtag).

So how am I to retain the orientation of the parts (particularly the HumanoidRootPart and the rest of the character)?

EDIT: ok then for some reason doing the cframe stuff (without the angul stuff) also destroys the train, wat

EDIT EDIT: nvm i fixed it just needed to use :SetPrimaryPartCFrame()

now i just need to deal with the humroot cframe

oh wait

just do the same thing l.m.a.o

beep boop testing

#oh wait

it makes no difference

thnx roblox

1 answer

✓ Accepted answer

You can get the rotation matrix components and set the new CFrame with those values, as well as put in your new position values.

local x,y,z,r00,r01,r02,r10,r11,r12,r20,r21,r22 = YourCFrame:components()
local newX = --Your new x value
local newY = --Your new y value
local newZ = --Your new z value

local newCF = CFrame.new(newX, newY, newZ, r00,r01,r02,r10,r11,r12,r20,r21,r22)

1 comment

0

Thank u!

This discussion is preserved as it was. New answers, comments, and votes are closed.