How do i make a part move to a player (5 studs above)

i want to make a part that follows the player 5 studs above.
Basically the part is in RS, and the script needs to be in the part. i cant put it in character scripts
Why?
Because i need to tell the part EXACTLY who to go to. i am not trying to make it find the nearest player and follow them.
Thats all, Thank you if you can help!

2 Likes

This is a snippet of code that will let you select a target player and the part will follow them.

local TargetPlayer = game.Players.Roblox -- Roblox is an example, update this line to the target player.
local Character = TargetPlayer.Character
local Head = Character:WaitForChild("Head")

local HeightAboveHead = 2 -- amount of space from head to part in studs

local HeadPosition = Head.Position -- head's position (holy guacamole! no way jose!)
local PartPosition = Vector3.new(HeadPosition.X, HeadPosition.Y + HeightAboveHead, HeadPosition.Z)

local Part = script.Parent

-- Do styling for the part here.

while true do
  Part.Position = PartPosition
  wait(0.01) -- slight timeout to stop the game from lagging incredibly hard
end
1 Like

Also, please dont get mad at me. ive been scripting for month and im not entirley sure what im doing

1 Like

Also to be nice, i want to add something.
“While true do”
and then
“wait”

I would suggest doing
“While task.wait() do”

1 Like

These do not have a difference in execution time, this is effectively the exact same thing.

1 Like

It’s completly fine. Running the loops is completly based out of your preference, as long as it returns the same results.

2 Likes

i know, but its just easier in my opinion. fine if u dont wanna use that

1 Like

If a solution works, please mark it as the solution on your post so others can find the answer easier.

1 Like

im testing it rn, i had to fixa little bit of code to fit my exact needs.

1 Like

Just tested this, the part teleports to my player, starts rapidly spinning, and wont follow me.

1 Like

To add, i dont want it to teleport to the player, i want it to move 5 studs above the player.
TBH i like how it stays in one place though

1 Like

Hello Bungar it’s important to be more clear in what you are trying to achieve. That said in your earlier post before this one, I have given you a more efficient solution. It doesn’t require using loops, and instead just requires inserting an attachment into your players root. To make it follow your player from above you can just change the position of the attachment in the players root and it will follow.

2 Likes

Bug support resource

You have specified you want it to teleport to the player.

Please be specific in creating new topics as to help problem solvers figure out what you need help with.
Additionally, the Roblox DevForum is only a resource, and is not suit to develop entire scripts for you. Bug fixing and additional modifications are something you do yourself.

i said move to, as in move.
Just to add, when you move to a place IRL, do you teleport to it?

I have edited my scripts again. And I tested it; It works.

local object -- path to object

local runService = game:GetService("RunService")
local offset = CFrame.new(0,5,0) -- set this to anything you want
local alpha = 0.3 -- same with this one

local targetPlayer = game.Players:WaitForChild("AlaTomKing")
local character = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
local Head = character.Head

object.CFrame = (Head.CFrame * offset)
runService.Heartbeat:Connect(function()
	--print(humanoidRootPart.CFrame * offset)
	object.CFrame = object.CFrame:Lerp(Head.CFrame * offset, alpha)
end)

This is to be put in the ServerScriptService (or in the part as you have asked) and you may replace the Head with HumanoidRootPart to exclude the weird animation the object makes. But then you may need to increase the offset a bit to match “5 studs”.

1 Like

like i said in my post (please read it)
Script must be in the part
Player must be able to be chosen (I see you have done that though.)

Yo no offense but I’m starting to think you’re part AI or something.

1 Like

Do not demand things from people as you say here. It is super easy to modify and finish this requirement yourself.

Please keep these in mind for future posts.

Then you can put the script inside the part. I tested this and came back with the same results.

1 Like

idk man i was told to be formal :sob:
Idk what to do anymore, people keep not understanding stuff
i can proove im not idk??