Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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:

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

2 answers

✓ Accepted answer

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.

local player = game.Players.LocalPlayer
local character = player.Character
local dexbot = script.Parent
local pos = dexbot.BodyPosition
while true do
	wait()
	pos.Position = (char.Head.CFrame * CFrame.new(0,3,2)).p
end
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.

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local dexbot = script.Parent
		local pos = dexbot.BodyPosition
		while true do
			wait()
			local c = char.Head.CFrame * CFrame.new(0,3,2)
			pos.Position = c.p
		end
	end)
end)
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.

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local dexbot = script.Parent:Clone()
		dexbot.Parent = workspace
		local pos = dexbot.BodyPosition
		while wait() do
			pos.Position = (char.Head.CFrame * CFrame.new(0,3,2)).p
		end
	end)
end)
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.

#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...

local player = game.Players.LocalPlayer
repeat wait() until
player.Character and script.Parent
local character = player.Character
local dexbot = script.Parent:clone()
local pos = dexbot:WaitForChild("BodyPosition")
dexbot.Parent = workspace
while true do
wait()
local head = character:WaitForChild("Head") -- Variables are never a bad thing unless they're global.
pos.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.
end

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

This discussion is preserved as it was. New answers, comments, and votes are closed.