Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Spoookd 32
5 years ago
Edited 5 years ago

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

Log in to vote
2
Answered by 5 years ago

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! :)

0
Definitely the better choice.  Zafirua 1348 — 5y
0
It worked! Thanks so much! Spoookd 32 — 5y
0
No problem :) Operation_Meme 890 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
I got no error and it didn't work. Spoookd 32 — 5y
0
ok I'ma edit BlackOrange3343 2676 — 5y
0
What does it print BlackOrange3343 2676 — 5y

Answer this question