How To Create Moving Platforms In Roblox
You can access the full course here:Intro to Roblox Game Making
In this lesson, we’re going to move our platform up and down.
To start off, we need toaccesstheMovingPlatformas we’ve done in the previous lesson, andstoreit into avariable:
local part = script.Parent
Using the TweenService
Next, we’re going to use aTweenServicewhich is a Roblox service we can use tomoveour object:
local part = script.Parent
local tweenService = game:GetService(“TweenService”)
When we type in “game” we’re accessing the entire game that we are currently in, and by doing so we canrequirethe TweenService we need.
Defining our Tween with a TweenInfo
Following, we’re going todefinehow we want themovementto be in theTweenInfo:
local part = script.Parent
local tweenService = game:GetService(“TweenService”)
local tweenInfo = TweenInfo.new(3.0, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, true, 0.2)
Thefirstparameter stands forhow longit’s going to take to move the platform between two positions, in our case3 secondsto go frombottom to top.
Secondly, we have theeasing style(whether the movement is going to be smooth, linear, or random). By choosing thelinearoption, we’re stating that the platform will move up and down at the exactsamespeed.
The third parameter is theeasing directionand we choose ‘Out‘ as we want it to move nice and straight along asingledirection.
The fourth parameter is therepeat count, which is basically how many times this tween is going to repeat itself. As we want it to repeat for as long as the game isactive, we enter “-1“.
The fifth parameter defines if it’s going to bereversingor not. We’re setting it totrueas we want it to reverse.
Finally, the last parameter is thedelay time. Once it reaches, the platform’s going to wait0.2 secondsbefore going back to the start.
Assigning the Tween to our MovingPlatform
We’re then ready toassignthetweento ourMovingPlatformpart:
local part = script.Parent
local tweenService = game:GetService(“TweenService”)
local tweenInfo = TweenInfo.new(3.0, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, true, 0.2)
local tween = tweenService:Create(part, tweenInfo, {Position = part.Position + Vector3.new(0, 20, 0)})
Note that we’re using theTweenServicewe’ve created toconnectourpartto theTweenInfowe’ve specified above. Lastly, we give it apositionto move the platform to which is itscurrentpositionplus20metersupin the air.
Now, we just need toplaythetween.
We can see that it is working just as we’ve specified in our script:
Overall, that is how youset upan obstacle course in your Roblox game. You canaddin more checkpoints, platforms, and moving platforms as you wish.
In the next lesson, we’ll be going over how to publish our game.
Transcript
Welcome back everyone. In this lesson we are gonna finally be moving our platform up and down, okay. Now, in terms of how advanced this is gonna be, it’s gonna be quite a bit more advanced than what we’ve done previously, but we’re gonna go step by step and have a look at how it is done, okay.
So, first of all, what we need to do is have our script open right here and I want you to go ahead and delete all of the code that you’ve currently written, if there is anything, okay. So make a nice blank file and we can begin.
So, first of all, what we need to do is access our Parent part, okay, the moving platform, we need to access this. And we went over this in the previous lesson. All we need to do is go script.Parent to access it.
But in this case, we are gonna assign this to a variable, okay, so every time we’re going to access the Parent part, we don’t have to go script.Parent, okay? Because that’s gonna make our code longer than it needs to be. So what I’m gonna do is put this into a variable and I’m going to go local part equals script.Parent.
So now whenever we write down the word part this is basically going to be referencing our script.Parent. So we can go part.position, for example and we can modify the position that way. All right, nice and easy. Next, what we need to do is access something known as a Tween Service.
Now, a tweenService, okay, there’s two words there, Tween and Service. Tweening is basically, in programming and in-game development, the act of basically moving or rotating an object over time, okay. Animating an object to move to one position to another, or rotate to one position, or rotate to another rotation, for example.
And what we’re going to be doing is using tweening in order to move an object up and down, okay. And a service in ROBLOX here is basically something that is on the server-side and it basically allows us to do certain things, okay. For example, we have the player service, we have our team service, we have our chat service, our localized service, sound service.
It’s basically a lot of different services that we can access. And the Tween Service is going to be what allows us to set up these tweens. So I’m going to do is assign it to a variable. I’m going to go local. I’m going to call this one tweenService and this is going to be equal to, game. Now, when we write down game, this is basically accessing the utmost of the hierarchy.
This is accessing the entire game that we’re currently in. We then went to put in a colon and we went to write down, GetService, okay. And GetService requires basically a name for the service. So, game:GetService with a capital G and a capital S, and then inside of the two parentheses we need to give it a string of the service name.
Now, if you put down the two quotation marks, you can see that a list of all these different services pop up and you can see that with ROBLOX there is a very large number of them. Now, the service we need is going to be the tweenService, like so, okay. It should pop up in the list when you’re typing it down.
So you can just hit enter to auto-fill it, like so. And this is going to access the tweenService which is going to allow us to move this object over time. So, then what we need to do is go down to a new line and we are going to create another local variable.
And this variable is going to contain the information about our tween, okay, because we need to define things such as how long is it going to take to move between the two positions, do we want to smooth in? Do we want it to have a repeating action, so does it want to bounce back and forth?
And also what is the delay of when it reaches the end to returning to the start? You know, is it going to wait there for a couple seconds, then keep moving? This is all the things that we need to define in the tween info. So for this, we’re going to go local.
I’m going to call this one, tweenInfo equals TweenInfo, so, capital T, capital I, and then go, .new . And then inside of two parentheses, we need to define some values that we need to basically assign to this tween. The first number is going to be how long is this platform going to take to move from one position to another?
In our case, let’s just say it’s going to take three seconds, okay. So to move from bottom to top, it’s going to take three seconds. So, I’m going to write 3.0 for three seconds, put a comma. And as you can see what pops up is a text basically defining all the properties or the parameters that we need to give it, okay.
Parameters are basically values that we feed into a function, which this is a function. And so we’ve got the time, next we need the easing style. And the easing style is basically, is it going to be smooth? Is it gonna be linear? Is it going to be random, for example. Now we want it to just be moving up and down, you know, as normal.
So we’re going to go, Enum.EasingStyle. and as you can see here, we have a bunch, we have Circular, Cubic, Elastic, Linear, Quad, Quart, Quint. I’m just going to choose Linear, okay. So it’s going to be moving up and down at the exact same speed. We can put another comma now, and we need to find the easing direction, okay. I’m going to go, Enum.EasingDirection.Out.
Okay, we just want it moving nice and straight, along a single direction. Next, we need to find the repeat counts. So how many times is this Tween going to repeat itself? Now we want to repeat itself, you know, for as long as the game is active.
So we can enter in a large number such as 999,000 million, for example, whatever you want. But a much easier way is to add negative one. And this basically means that it’s going to run forever. Okay, another comma. We need to assign a true or false value for if it’s going to be reversing or not. Now we want it to reverse.
So we’re going to add in true and finally another comma and this is going to be the delay time. Okay, so once it reaches the end, how long is it going to wait there before going back to the start? Now let’s say we want it to be 0.2 seconds, okay. So it’s only going to stay there for a fraction of a second and then move back.
So that is our tween information set up, okay. There’s a lot of things here. Next up, what we need to do is assign the actual tween. Okay, we need to actually create this tween because right now we’ve just created basically the data or the information for this tween.
Now what we need to do is basically tell it, okay, we want to you to apply this tween here to this specific part and we want you to move it to this specific position. Okay, let’s do that now. What we’re going to do is we’re going to go, local tween equals tweenService, so we’re accessing this variable right here, we then want to go, :Create and we need to give it a few variables.
First of all, we need to give it the actual part that we want to be tweening, okay. In our case, that’s going to be the script.Parent which we’ve assigned to the part variable here. So we can just enter in part, like so. So that is the thing that we want to be moving. Next, we need to give it a tweenInfo.
So we’ll send over the tweenInfo right here. Then what we want to do is give it a position to move to. Now the position that we want it to move to is basically going to be this part’s existing position, plus, you know, 20 or so meters up in the air. So for this, what we need to do is add in two squiggly brackets, like so.
And what we’re going to do here is just go, {Position = part.Position + Vector3.New and then two more parenthesis and we need to find a vector to add to that position. Now it’s going to be zero on the X 20 on the Y and zero on the Z, okay. So basically what we’re doing here is assigning the position or the tween position, so where we want it to tween to, to basically be our part’s existing position, plus 20 in the air, okay, 20 vertically.
So it’s going to be moving directly up. And then once it reached that, it’s going to move down back to its origin. Okay, and finally, after all of this, we just need to go tween:Play okay, just like that, two parentheses at the end. And that is going to play the tween. So, this is the code right here, okay.
It can be quite confusing, I do understand that but that is how we can tween an object up and down. So, save that, press play, and if we look over at our platform now, we should see that it is moving up and down, which it is. Let’s hop into server mode, and we can see that the platform’s moving up and back down and you can see when it reaches the bottom it waits 0.2 seconds and then moves back up again.
So yeah, as you can see, it’s got the origin position at the bottom, and then it is moving 20 up into the air. Once it reaches that point, it goes back down and it basically just keeps on doing that forever since we did assign the repeat count to be negative one.
Okay, so, that is our moving platform set up and ready to go. You can, of course, add in multiple more if you wish. But yeah, basically that is the obstacle course set up for ROBLOX. You can, of course, add in many more checkpoints, many more different platforms.
But overall, that is how you set up an obstacle course with checkpoints, platforms, and moving platforms, in your ROBLOX game. Now in the next lesson, we are going to be going over how we can actually publish this publicly so we can then play our game. So thanks for watching and I’ll see you all then in the next lesson.
Interested in continuing? Check out ourall-access planwhich includes 250+ courses, guided curriculums, new courses monthly, access to expert course mentors, and more!}