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

How to keep a part in front of the player while they are moving?

I have made a part that spawns in front of the player while they hold Z, and the only way I got it to stay there was when I made it anchored, though I want to make it stay in front of the player while the player moves.

while KeyHeld == true do
	power.CFrame = player.Character.Head.CFrame*CFrame.new(Vector3.new(0, 5, -1.5))
end

But all it does is sit in the middle of the map. Before I used the while loop I just had it anchored so that's the only way I got it to go in front of the player when the button is pressed, but when I use the while loop, having it anchored or not anchored doesn't make much of a difference.

2 answers

✓ Accepted answer

Actually, in my opinion, there is a better way of doing this than using a loop: welds. By using welds, you have no use for a loop and it will not seem choppy, but rather smooth.

It is actually fairly simple as well:

-- Example welding of a part with a 5 stud offset from another part

function weld(part1, part2)
	local weld = Instance.new("Weld", part1)
	weld.Part0 = part1
	weld.Part1 = part2
	weld.C0 = CFrame.new(0, 0, 5)
end

With the example given above, you should be able to easily figure out how to implement it for your purposes.

Make sure to accept my answer if it helped! :)

3 comments

0

Definitely the better choice. 

0

It worked! Thanks so much!

0

No problem :)

Try:

local Char = player.Character or player.CharacterAdded:Wait()
while KeyHeld do
	wait()
	print('Looping')
	print(Power.CFrame)
	power.CFrame = CFrame.new(Char:WaitForChild('Head').Position)*CFrame.new(0,0,-5) 
	print(Power.CFrame)
end

Hopefully this helped

If it doesn't work open up Output or press F9 and tell me if there is error

3 comments

0

I got no error and it didn't work.

0

ok I'ma edit

0

What does it print

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