Can someone tell me what this line of code does? (Toolbox code review)

What exactly does this code do?
I think gets the position right? (idk)

but that I’m trying to say here is I want to understand the code so I may use it later on when i might need it.

How does this work? Your subtracting CFrame.Position from CFrame? uhhhh… huh?
Is there maybe a mathematical problem someone can explain plz plz

local currentrotation = tor.CFrame - tor.CFrame.Position

Here is the full loop.

for i = 0, dist, 1 do
	local currentrotation = tor.CFrame - tor.CFrame.Position
	tor.CFrame = CFrame.new(tor.Position + unit * dist / 25) * currentrotation
	wait()
end

its removing the position of the cframe so that its an orientation only

2 Likes

What does tor.Position + unit do?

is it a math equation? or is it doing the same thing as tor.CFrame - tor.CFrame.Position?

It’s hard to know what tor is and what other variables in the code mean with these snippets, but it just seems to be the code for rotating the models in the Viewport tabs.


Nevermind that, I thought it was the Toolbox’s plugin code. Yes, the first thing simply removes the position from the CFrame (interesting approach) and the second thing just acts as a weird Tween replacement?

1 Like

a unit vector is a vector with its magnitude equal to 1, its basically a direction without distance, tor.Position + unit will make that direction relative to tors position

1 Like

Its supposed to me a tornado but idk why its tor cause its a little confusing. This model was made like a very long time ago.

Looks like it’s trying to rotate the object in a full 360 by breaking it down to 4 steps.

1 Like

I’ll just let yall look at the full script here cause it seems that its a little outdated and might not be a reliable way to make a tornado (tor)

local tor = script.Parent
local mesh = tor.Mesh
local rootpos = tor.Position

local restingTime = math.random(1, 3)
local maxMovingDistance = 10
local maxAllowedRange = 50*20

local spin = coroutine.resume(coroutine.create(function()
	while wait() do
		tor.CFrame = tor.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(-2), 0)
	end
end))

------------------------------------------------

local move = coroutine.resume(coroutine.create(function()
	while wait(restingTime) do
		local pickedpos = tor.Position + Vector3.new(math.random(-maxMovingDistance, maxMovingDistance), 0, math.random(-maxMovingDistance, maxMovingDistance))
		local dist = (pickedpos - tor.Position).magnitude
		local unit = (pickedpos - tor.Position).unit
		print(dist)
		print(unit)
		-- move the tornado in a random position
		for i = 0, dist, 1 do
			local currentrotation = tor.CFrame - tor.CFrame.Position
			tor.CFrame = CFrame.new(tor.Position + unit * dist / 25) * currentrotation
			wait()
		end
		--check if the tornado goes too far, if so it will go back to it's root position
		if (pickedpos - rootpos).magnitude >= maxAllowedRange then
			local ldist = (pickedpos - rootpos).magnitude
			local lunit = (pickedpos - rootpos).unit
			print("Tornado went too far, going back (distance: " ..ldist.. " studs away)")
			for i = 0, ldist, 1 do
				local currentrotation = tor.CFrame - tor.CFrame.Position
				tor.CFrame = CFrame.new(tor.Position - lunit * ldist / (ldist*0.75)) * currentrotation
				wait()
			end
		end
	end
end))

This is a model from toolbox

Yes, I love to figure these out… This seems to be part of a Tornado set up. I put what I thought belonged together from tool box parts and it kind of works… Enough to call it a Tornado set up.
Tornato.rbxm (5.7 KB)

I’d say this is a Tornado that is meant to move in a random direction then move back and repeat.
I made it a large one but it could have even been smaller ones too.

1 Like

Yeah, Its meant to move in a random direction but if it strays too far it would move back and repeat the process.

This type of thing is really good for round based games like Natural Disasters Survival.

1 Like

I know what the code does in general but its just I want to understand so I can make things on my own. I usually get confused by things like CFrame + unit or some other things like that cause i don’t know how they work

I highly recommend TweenService. It is much smoother and more reliable. You don’t have to worry about it going to far and moving back to the middle, as it just smoothly moves to the position given, even if its random.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.