Understanding CFrame:ToWorldSpace() | Programmer Tutorial

Introduction

Hello there, Today we are going to talk about CFrame:ToWorldSpace() function,
This function is usefull in multiple shooting or another cool effects!

What is position and origin of the world

Good question but before we start with :ToWorldSpace() I need to explain you what is Vector3 position and the origin of the world

So basically origin of the world is 0,0,0, When we look at the start baseplate the plate is on position 0,-10,0 which is basically 10 studs under the origin of the world

In another case, We can say that player position is 10 studs up, 25 studs left/right,14 studs forwad/at the back

You can find more information about Vector3 there
Vector3 Documentation

If you understand this we can get right into the :ToWorldSpace() function!

So what is :ToWorldSpace() - New method

:ToWorldSpace() is a function that can be called only when using CFrame

Example:

workspace.Part.CFrame:ToWorldSpace()

What arguments this functions takes?
Well this function takes one argument and that is offset CFrame of the part , Let me explain

Let’s say you have part on position 20,50,30, So when you use function :ToWorldSpace() and you enter some offset position this will happens:

The position of the part will become 0,0,0 so origin of the world, but only for script not for world
Then let’s said you enter CFrame 0,10,0 so the function will return CFrame with position 20,60,30

What this did it just added on Y coordinates 10 studs so it’s like you will do

PartPosition = PartPosition + Vector3.new(0,10,0)

So you will use the function like this:

local newCF = workspace.Part.CFrame:ToWorldSpace(CFrame.new(0,10,0))

You can find more information about CFrame included with :ToWorldSpace() function too
CFrame Documentation

Also :ToWorldSpace() but in older method!

Before the :ToWorldSpace() was added, The programmers was using

local newCF = cf1 * cf2

This is just the older method of it, But it is doing the same thing as I covered in the newest version

So the CF1 is the CFrame we are offsetting with and the CF2 is the CFrame offset

So again when will we do

local NewCF = CFrame.new(50,50,60) * CFrame.new(0,10,0)

The NewCF will be 50,60,60, Is is the same behavior as we got in the newest version

What is it usefull for?

Good Question, Let’s say you calculated position of where the mouse CFrame is,

So you can make another position offseted like this:

local OffsetedCFrameToRight = MouseCFrame:ToWorldSpace(CFrame.new(10,0,0))
local OffsetedCFrameToLeft = MouseCFrame:ToWorldSpace(CFrame.new(-10,0,0))

local leftpart = Instance.new("Part")
leftpart.Anchored = true
leftpart.CanCollide = false
leftpart.CFrame = OffsetedCFrameToLeft
leftpart.Parent = workspace

local rightpart= Instance.new("Part")
rightpart.Anchored = true
rightpart.CanCollide = false
rightpart.CFrame = OffsetedCFrameToRight
rightpart.Parent = workspace

Results:

Or some type of weird thing like I did, If you want to play it I will give you link here:
The game
To use it just click somewhere it will create some type of beam :smiley:

Conclusion

Now it depends on you if you will be using the new method or the older, But both of these methods are doing the same thing, I just add that for people who want know more!

Thanks for reading this tutorial and I hope this will help you!
In soon I will be creating new topic about :ToObjectSpace() which is similar to :ToWorldSpace()
If you found out some mistake in this guide, Let me know in the comments

Anyways bye! :wave:

16 Likes

i am really new to programming so can you please explain what CFrame means. i heard a lot of people talk about it buy i never got to know what it actually means. :sweat_smile: sorry if i lack some IQ i am very new to programming.

Thank you
Ranger

1 Like

This will help.
https://developer.roblox.com/en-us/articles/Understanding-CFrame

1 Like

This is a simple tutorial and I learnt something, thank you!

I’ll be annoying and say:

You should have said it like this:

PartPosition += Vector3.new(0, 10, 0)

2 Likes

Yes but this is luau not lua, But it is option too

1 Like

what is the actual use of it anyway like does the function convert positions into …???

1 Like

It is for calculation new position with one existing position

1 Like