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

(SOLVED) coroutine function only prints the magnitude once?

Code:

game.Players.PlayerAdded:Connect(
    function(p)
        local cor =
            coroutine.wrap(
            function()
                while wait() do
                    local character = p.CharacterAdded:wait()
                    local mag =
                        (character:WaitForChild("HumanoidRootPart").Position - workspace.MagnitudeBlock.Position).Magnitude
                    print(mag)
                end
            end
        )
        cor()
    end
)

When I join, it only prints the magnitude once, how can I fix this?

1 answer

Solved it using CharacterAdded instead of player.CharacterAdded:Wait()

game.Players.PlayerAdded:Connect(
    function(p)
        p.CharacterAdded:Connect(
            function(character)
                local cor =
                    coroutine.wrap(
                    function()
                        while wait() do
                            local mag =
                                (character:WaitForChild("HumanoidRootPart").Position - workspace.MagnitudeBlock.Position).Magnitude
                            print(mag)
                        end
                    end
                )
                cor()
            end
        )
    end
)

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