Hello, This sounds like a relatively easy thing to do, yet I've somehow managed to waste quite a bit of time trying to make the humanoid spin in a complete circle.
What am I doing wrong here? I know its that second half from this line that needs to be rotating the humanoid, but it instead turns the player towards a direction.
1 | CFrame.new(character.PrimaryPart.Position) * CFrame.Angles( 0 ,math.rad( 360 ), 0 ) |
Thanks!
It's because you rotated the character
directly at 360 degrees. To spin, you need to gradually increase the rotation until it reaches 360. You can achieve that using a for
loop.
In case if you don't know what a for loop does, it has two types: for number = start, finish, increment do
and for ... in function do
.
The first one simply adds increment
to number
from start
until number
is equal to finish
.
01 | for number = 1 , 10 do -- if increment is not set, it is automatically set to 1 as the default |
02 | print (number) -- prints 1 up to 10 |
03 | end |
04 |
05 | for odd = 1 , 9 , 2 do |
06 | print (odd) -- prints all odd numbers from 1 to 9 (1, 3, 5, 7, 9) |
07 | end |
08 |
09 | for even = 2 , 10 , 2 do |
10 | print (even) -- prints all even numbers from 2 to 10 (2, 4, 6, 8, 10) |
11 | end |
The second one is mostly used for tables. It gets the returned values from the function.
1 | function test() |
2 | return "hi" , "hello" , "Roblox" |
3 | end |
4 |
5 | for a, b, c, d in test do |
6 | print (a, b, c, d) -- hi hello Roblox nil |
7 | -- the fourth prints nil because a fourth value wasn't returned by the function |
8 | end |
The second one is mostly used for tables is because of the pairs()
function. This guy explains better of what does pairs()
do.
01 | local tbl = { |
02 | [ 1 ] = "hi" , |
03 | [ "hello" ] = "Roblox" |
04 | } |
05 |
06 | for a, b in pairs (tbl) do |
07 | print (a, b) |
08 | -- 1 hi |
09 | -- hello Roblox |
10 | end |
Now that we know what is a for
loop, we can now use it:
1 | local characterCFrame = character:GetPivot() -- similar to character.HumanoidRootPart.CFrame |
2 | for rotation = 1 , 360 do |
3 | character:PivotTo(characterCFrame * CFrame.Angles( 0 , math.rad(rotation), 0 )) -- PivotTo is the same as setting/changing a part/model's CFrame |
4 | task.wait() -- we will add this just incase if it breaks the game |
5 | end |
6 | character:PivotTo(characterCFrame) -- returns to original rotation |