How To Use BodyPosition

From Legacy Roblox Wiki
Revision as of 13:53, 23 September 2008 by >Davidii (Ever wondered how the Vortex works?)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

How to use BodyPosition

In this tutorial I expect you to be an intermediate scripter and builder. If you are not, you will probably not be able to do this tutorial. ALSO you should be in ROBLOX STUDIO.


Alright, let's begin. First, make a small brick. I made mine 5x1x5. Next, select the brick you just made and click Insert->Object->Script. Now double-click the new script object. You should see the following:

 print("Hello World!") 

Delete it, you won't need it. First, add this code.

 function onTouched(hit) 

This will detect when something touches it, and what. Pretty basic. Let's continue.

local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then

This will detect wether what touched it was a human or not. You don't want a rocket or slingshot to activate it do you?

local b = Instance.new("BodyPosition")

Ok, this defines our Bodyposition. But right now it doesn't do anything, because it doesn't have a parent.

b.position = (500, 500, 500)
b.maxForce = (500000000, 500000000, 500000000)
b.Parent = hit.Parent.Torso

This will define some of BodyPosition's properties. I'll explain each one. BodyPosition's position property does this: When a BodyPosition has a parent, it will make the parent fly to the BodyPosition's position property. It's great for a lot of things, like teleporting without teleporting. LOL. Next we have the maxForce property. Simply put, it's how much force can be put on each axis of the parent brick. It also governs how far away the BodyPosition can be from it's position property before it will take a brick there. Finally, the Parent of the BodyPosition is what the BodyPosition will be moving, in this case, the character's torso. So far we have this:

function onTouched(hit)
  local h = hit.Parent:findFirstChild("Humanoid")
  if h ~= nil then
    local b = Instance.new("BodyPosition")
    b.position = (500, 500, 500)
    b.maxForce = (500000000, 500000000, 500000000)
    b.Parent = hit.Parent.Torso

Alright, this is great. You'll fly to the point. But when will you drop!? Thats a problem we have to fix. Let's try this:

wait(3)
b.Parent = nil

Great! Now after 3 seconds, the BodyPosition ceases to exist, and you will drop! Now we just have to clean up the functions, and actually connect it to the Touched property!

  end
end
script.Parent.Touched:connect(onTouched)

There we go! those ends end the if statement and the function. Let's see the whole script.

function onTouched(hit)
  local h = hit.Parent:findFirstChild("Humanoid")
  if h ~= nil then
    local b = Instance.new("BodyPosition")
    b.position = (500, 500, 500)
    b.maxForce = (500000000, 500000000, 500000000)
    b.Parent = hit.Parent.Torso
    wait(3)
    b.Parent = nil
  end
end
script.Parent.Touched:connect(onTouched)

Great! Your finished! If you want to change where the character flys to, then you can change the BodyPosition's position property. Also, something really fun is if you only wait(0.1) then the character goes flying! Try it out! Lolz. Well, that's the tutorial, thanks for reading!