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
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
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)
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 got all of the following information in the following places,
Good Luck!
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 Variable
s 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
~~ KingLoneCat