how to make an moving object in roblox

--[[ Aiden's Smart Code, make sure to learn from this and use it later on!
But before you copy this code you will need to do a few things, such as:
1. Insert an BodyPosition in the object.
2. Make an script in the object.
3. Make sure the object is just under Workspace.

There are multiple codes in here so make sure to get the right code!
--]]

-- Code 1, when a player joins the game the object will move:

local Object = game.Workspace:WaitForChild("Object Name Here")
local BodyPosition = Object.BodyPosition

game.Players.PlayerAdded:Connect(function(player)
	BodyPosition.Position = Vector3.new(Coördinates in these brackets)
    -- Use this if you want the object to move to another position after this!
    wait(5) -- Change this number to how many seconds you want to wait.
    BodyPosition.Position = Vector3.new(Coördinates in these brackets)
end)

-- Code 2, when a player touches another object, the object will move:

local Object = game.Workspace:WaitForChild("Object Name Here")
local TouchedObject = game.Workspace:WaitForChild("Touched Object Name Here")
local BodyPosition = Object.BodyPosition

function OnTouched(part)
	BodyPosition.Position = Vector3.new(Coördinates in these brackets)
    -- Use this if you want the object to move to another position after this!
    wait(5)
    BodyPosition.Position = Vector3.new(Coördinates in these brackets)
end

TouchedObject.Touched:Connect(OnTouched)

--[[ This is all there is, i hope you will have learned from this code
and that you will use it soon in your games!

If you'd like to thank me you can do this:
1. Learn from this code and become a better programmer!
2. Like this code!
3. Make an successfull game!
--]]

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source