Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I set a BodyPosition's position behind my character's head?

Asked by 8 years ago

I am making a little flying robot that follows you around. It's supposed to be 2 studs behind your character's head, but when I run the game, the bot goes to the default position. I don't know why this is happening, it should work. Please help. Here's the local script:

1local player = game.Players.LocalPlayer
2local character = player.Character
3local dexbot = script.Parent
4local pos = dexbot.BodyPosition
5while true do
6    wait()
7    pos.Position = character.Head.CFrame.lookVector - Vector3.new(0,0,-2)
8end

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Simply multiply the head CFrame by another CFrame

It's weird how this is calculated, but it fits best with your problem. Lookvector is a property of all CFrames, and it tells which direction the CFrame is facing.Lookvector can be vary useful, but when you want to get a position from a CFrame, you can multiply it by another CFrame.

In our case, we want the CFrame to go behind the had and slightly above it. A positive Z is behind, and a Negitive Y is above.

To make a CFrame a Vector3, we just use the .p after the CFrame which turns it into a position.

1local player = game.Players.LocalPlayer
2local character = player.Character
3local dexbot = script.Parent
4local pos = dexbot.BodyPosition
5while true do
6    wait()
7    pos.Position = (char.Head.CFrame * CFrame.new(0,3,2)).p
8end
Doesn't work? Keep reading.

Still isn't working?

This is because you're either A.) Using a Local Script in workspace, or B.) Using Local Player in a regular script.

To fix this, get the player with the PlayerAdded event, and get the character with the CharacterAdded event. Also make sure to use a RegularScript.

01game.Players.PlayerAdded:connect(function(plr)
02    plr.CharacterAdded:connect(function(char)
03        local dexbot = script.Parent
04        local pos = dexbot.BodyPosition
05        while true do
06            wait()
07            local c = char.Head.CFrame * CFrame.new(0,3,2)
08            pos.Position = c.p
09        end
10    end)
11end)
If tested this and it works fine.

Cloning this to new players

Just use the Clone() function and set it to new players if you want them to all have the orb.

01game.Players.PlayerAdded:connect(function(plr)
02    plr.CharacterAdded:connect(function(char)
03        local dexbot = script.Parent:Clone()
04        dexbot.Parent = workspace
05        local pos = dexbot.BodyPosition
06        while wait() do
07            pos.Position = (char.Head.CFrame * CFrame.new(0,3,2)).p
08        end
09    end)
10end)
I tested this and it works fine.

I got all of the following information in the following places,

  1. eLunate's answer on how to get the position according to a CFrame
  2. Wiki Body Position
  3. Wiki CFrames
  4. Wiki PlayerAdded Examples
  5. Wiki CharacterAdded Examples
  6. Wiki Clone Function

Good Luck!

if I helped, please don't forget to accept my answer.
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

If you want the Part to always be behind your Character's Head you would have to use the :pointToWorldSpace(Vector3.new(x, y, z)) method in this case.

The first thing you want to do is set your Variables and parent the Part to the Workspace. I suggest you clone the Part and then parent the Part to the Workspace, if you want it to appear in the game. Then you would just use the while loop like you have and if you want it to be relative to the head at all times you will be using the method I mentioned above. I am going to give you a script here and then link you to a Roblox wiki that might explain it better or that might give you a better idea...

01local player = game.Players.LocalPlayer
02repeat wait() until
03player.Character and script.Parent
04local character = player.Character
05local dexbot = script.Parent:clone()
06local pos = dexbot:WaitForChild("BodyPosition")
07dexbot.Parent = workspace
08while true do
09wait()
10local head = character:WaitForChild("Head") -- Variables are never a bad thing unless they're global.
11pos.Position = head.CFrame:pointToWorldSpace(Vector3.new(0, 0, 2)) -- You can look into that method :pointToWorldSpace(Vector3.new(x, y, z)) further on the wiki. But, this script should work. I tested it.
12end

Here is the link for the Roblox wiki article

I hope I helped in one way or another. If you have any questions please, post a comment below!

Thanks,

~~ KingLoneCat

Answer this question