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

Vector3 and CFrame conversion?

Hiaa guys. Sorry again sorry for all the questions ;). Quick quesion here, about Vector3 and CFrame values. I want to know how to convert a value from CFrame to Vector3 and vice - versa. Here's an example:

wait(2)
blue = game.Workspace.BlueSpawn
red = game.Workspace.RedSpawn
Var = 0
players = game.Players:GetChildren()

function SpwnTele()
	print(player.Value.Value)
		if player.Value.Value=="blue" then
		player.Character.Torso.CFrame = CFrame.new(Vector3.new(blue.Position)) --heres the line I need help with
		print(player.Character.Torso.CFrame)
		else
		player.Character.Torso.CFrame = CFrame.new(Vector3.new(red.Position)) --and this one too
		print(player.Character.Torso.CFrame)
		end
	end

for i,v in pairs(players) do
	if Vteam==nil then
		player = v
		Vteam = Instance.new("StringValue")
		Vteam.Parent = v
		Var = math.random(1,2)
		if Var==1 then
			Vteam.Value = "red"
			SpwnTele()
		elseif Var==2 then
			Vteam.Value = "blue"
			SpwnTele()
		end
	end
end


Sorry if the script is a bit long. If you find a errors within the script, speak up! (BTW the script is supposed to create a string value for each player then pick a random team color, from which the player will be teleported to the corresponding Part with that color.) THX!!

1 answer

✓ Accepted answer

CFrame values have two Vector3 values in it:

1) The position the part is positioned at

2) The position that the part is facing

To get the position:

local pos = part.CFrame.p

To get where it is facing:

local pos = part.CFrame.LookVector

-- or, if you want individual numbers

local posX, posY, posZ = part.CFrame.X, part.CFrame.Y, part.CFrame.Z

5 comments

0

And how would I use this method of conversion in my script?

1

I just gave it to you. The values inside CFrame values are Vector3 values, so you don't really need to convert, you just need to index them.

0

Well... I guess it's good enough.

0

(Vector3 expected, got CFrame)or(CFrame expected, got Vector3) when i try this

0

Dude you're a GOD

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