Answered by
7 years ago Edited 7 years ago
Hi!
Remember that this is not a request site, but I can still take you through how the process is done.
Firstly, the best place for this script to go is in StarterCharacterScripts (this is inside StarterPlayer). This means the script will automatically go inside the player's character once they join the game.
Now, considering it is a child of the player's character, we need to identify where the object is going to follow. I recommend it to follow the HumanoidRootPart. It is basically the same thing as the Torso, but R15 characters don't have a child called "Torso", there is only UpperTorso and LowerTorso.
local torso = script.Parent.HumanoidRootPart
Now it is time to create the object that will follow the player using Instance.new. We can then set its properties to our liking.
1 | local center = Instance.new( "Part" ) |
2 | center.CanCollide = false |
3 | center.Size = Vector 3. new( 1 , 1 , 1 ) |
4 | center.Parent = script.Parent |
Nextly we need to add a BodyPosition. This is a BodyMover that applies a force to the specified object to a position in world coordinates. For this, we can make it apply a force to the character.
1 | local bp = Instance.new( "BodyPosition" ) |
2 | bp.maxForce = Vector 3. new( 9 e+ 100 , 9 e+ 100 , 9 e+ 100 ) |
Now it is time to create the loop that will get the object to follow the player! We can set the BodyPosition's goal to the player's torso now, so the object will follow the player.
2 | center.BodyPosition.position = torso.Position |
And that's it! You now have an object that follows the player. Here is the raw code. Make sure this is set in StarterCharacterScripts (located inside StarterPlayer).
01 | local torso = script.Parent.HumanoidRootPart |
03 | local center = Instance.new( "Part" ) |
04 | center.CanCollide = false |
05 | center.Size = Vector 3. new( 1 , 1 , 1 ) |
06 | center.Parent = game.Workspace |
07 | local bp = Instance.new( "BodyPosition" ) |
08 | bp.maxForce = Vector 3. new( 9 e+ 100 , 9 e+ 100 , 9 e+ 100 ) |
13 | center.BodyPosition.position = torso.Position |