Help on follow script (infinite yield possible hault)

hello roblox devs! i just need a tad bit of help with the following script…

local myPart = Instance.new("Part",workspace)
local myPos = Vector3.new(0,10,0)
local myLook = Vector3.new(0,1,0)
local myCFrame = CFrame.new(myPos,myLook)
myPart.Anchored = true
while(1) do --while loops are infinite.--
myPart.CFrame = myCFrame
codeprime8 = workspace:WaitForChild("codeprime8")
myLook = codeprime8.HumanoidRootPart.Position
myCFrame = CFrame.new(myPos,myLook)
wait()

end

i keep getting the error message “infinite yeild possible” even though im 1000% sure this code is correct. im a newer developer so dont critisize me to much.

Any help as soon as possible would be amazing!

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

:sweat_smile:

1 Like

One why do you have two waits, you have wait(1), and then a wait at the end of the while loop. Two, it’s probably waiting for your player to be found in the workspace… if I had to guess. I’m pretty sure that’s a warning, so it doesn’t error your code out, it just is infinitely waiting for something it isn’t finding.

1 Like

Are you sure that a “codeprime8” Instance is inside of the workspace? If not, then the WaitForChild would yield forever, and so you get that warning.

1 Like

wait as in a part in the workspace or the script being parented to a part named it?

1 Like

Waiting for the player model to exist in Workspace.

I’m not sure what the “codeprime8”( love the guy by the way ) Instance is supposed to be, but it doesn’t appear to be inside of the workspace from the screenshot you showed.

I’m referring to this line:

codeprime8 = workspace:WaitForChild("codeprime8")

yea. to be honest im reading from a book right now to learn so im not 100% what it is either. what i do is copy what he wrote, see his explanation, then try it on my own. he never said to add and parts or add it as a child to any parts in the workspace like he normally does so im just confused why it got an error.

You’re better off waiting for a player join the event, and then wait for character load, and then do your loops on the player once they’re loaded in.

how would i write that part of the script? game.workspace:WaitForChild(“humanoid”)
if hum then?

Since its in a While loop i would Recommend using :FindFirstChild Instead

also WaitForChild will halt the script until a object named “codeprime8” is in there
if your waiting for a player named “codeprime8” then use

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        -- do your stuff here
    end
end

Edit: heres some links CharcterAdded and PlayerAdded and put char in the brakets of the CharacterAdded function

You need to replace “codeprime8” with your own username for the example to work as that book intended.

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
    local myPart = Instance.new("Part",workspace)
    local myPos = Vector3.new(0,10,0)
    local myLook = Vector3.new(0,1,0)
    local myCFrame = CFrame.new(myPos,myLook)
    myPart.Anchored = true
    while(1) do --while loops are infinite.--
    myPart.CFrame = myCFrame
    user = char
    myLook = user.HumanoidRootPart.Position
    myCFrame = CFrame.new(myPos,myLook)
    wait()

    end
  end
end
1 Like

what are you talking about? i literally just changed your code and put it into the characteradded function [when your player is added to the game/worksapce… basically when you see yourself. the only thing that would need to be done is ensure the player actualy exists incase they leave, or die. other than that, that’s exactly what you were asking for.

and I’m not allowed?? how so…

nevermind with it.
i thought the person of the book was saying for the part to follow the player but it just looks at them. Thanks for the help! you guys are amazing. thank you for your time! it works now.

1 Like

Don’t want to bump your code to much, but while loops are not meant to be infinite. It creates memory leaks which can disrupt gameplay and induce more lag. RunService.RenderStepped is a good alternative. Also yielding nothing is bad practice. If you need a better understanding on why I say this, I have created a post discussing this bad practice which can be found here:

This is being run on the server, therefore RenderStepped won’t work without this being moved to the client-side. Not to mention, it sounds like they’re working their way into learning. Adding too many concepts right now will only make their learning process very difficult right now.

That is why I told the OP that. When they are learning, it is easier to learn not to use bad practice. That is because they haven’t used it a lot since they are learning. RenderStepped is for the client and Stepped and Heartbeat is for the server.

And you recommended RenderStepped for server-side. They already said they don’t understand function connections, and it sounds like they’re reading a book on Roblox development. I would let them mature a bit before you add too much into this.