Simple c++ function crashes game

This crashes the editor like 8 times out of 10

int32 UFL::RandomIntFromTable(TMap<int32, int32> Table)
{
	if (Table.Num() < 2) return *Table.Find(0);

	int32 totalRand = 0;
	TMap<int32, int32> LowerWeights;
	for (auto &Elem : Table)
	{	
		LowerWeights.Add(Elem.Key,totalRand);
		totalRand += Elem.Value;
		
	}
	
	int32 rand = FMath::RandRange(0, totalRand);
	int32 top = 0;
		
	for (auto &Elem : LowerWeights)
	{		
		top += *Table.Find(Elem.Key);
		if (rand < top)
			return Elem.Key;
	}

	return 0;
}

How do I fix this?

Do you garuntee that Table always has an entry with the key of 0? You might want to read up a bit on how TMap works versus TArray here: TMap | Unreal Engine Documentation

Look at what TMap::Find returns and how you could re-work this line to return the only entry in the map.

if (Table.Num() < 2) return *Table.Find(0);

oh im stupid, i wanted to get the first element of the map…