roblox-tsr
roblox-ts2mo ago
30 replies
CahwarDev

getAllComponents and onComponentAdded don't work

why could Flamework's getAllComponents always return empty table and onComponentAdded never work? With any class provided. Even though I'm sure there are some components in the game as I can see them if I print the object that's being returned with Dependency<Components>?
Solution
Getting id from the generic type and passing it as a component specifier solved the issue

/** @metadata macro {@link Flamework.resolveDependency intrinsic-flamework-rewrite} */
export function useAllComponents<T extends BaseComponent>(id?: IntrinsicSymbolId<T>): T[] {
    assert(id, "id must be defined");

    const componentDependency = useFlameworkDependency<Components>();

    const [components, setComponents] = useState(componentDependency.getAllComponents<T>(id));

    useMountEffect(() => {
        const addedConnection = componentDependency.onComponentAdded<T>((component) => {
            setComponents([...components, component]);
        }, id);

        warn(componentDependency);

        const removedConnection = componentDependency.onComponentRemoved<T>((component) => {
            setComponents([...components.filter((value) => value !== component)]);
        }, id);

        return () => {
            addedConnection.Disconnect();
            removedConnection.Disconnect();
        };
    });

    return components;
}
Was this page helpful?