# How do we remove player stats from weak map

**URL:** <https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [July 9, 2024, 9:01am UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475 "2024-07-09T09:01:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![q\_e\_e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@q\_e\_e](https://forums.unrealengine.com/u/q_e_e)\
**Post date:** [July 9, 2024, 9:01am UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/1 "2024-07-09T09:01:03Z")

</div>

Hello, I am having trouble to remove the player saved data when the player leaves the game I found these two videos

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/2/0/a20caadfc4f1514b4a5824c89c7d5ab695e48580.jpeg "How To Track Eliminations And More With Verse In UEFN") ](https://www.youtube.com/watch?v=_NL5VMM5Ipw&t=912)

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/7/7/b77a0d6f61ca38abe44155ab8f5b839db92304f2.jpeg "Remove an Element from a Map in Verse Code") ](https://www.youtube.com/watch?v=CYv4dGObK7k)

and the problem is they use a normal type of map, but I am using a weak map to keep track of the stats so is there a possible way to delete the stats when the player leaves?

````auto
OnPlayerRemoved(OutPlayer:player):void=
        if (ActualPlayer := PlayerStatsMap[OutPlayer]):
            var TempAllPlayersMap:[player]player_stats_table = map{}
            for (Key -> Value : PlayerStatsMap, Key <> OutPlayer ):
                set TempAllPlayersMap = ConcatenateMaps (TempAllPlayersMap, map{Key => Value})
            set PlayerStatsMap = TempAllPlayersMap```
````

---

<div class="post-metadata">

**Author:** ![Kasity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kasity](https://forums.unrealengine.com/u/Kasity)\
**Post date:** [July 12, 2024, 11:00pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/2 "2024-07-12T23:00:52Z")

</div>

If you don’t want to save player data between sessions or rounds you should use a normal map. But if you want to use a weak\_map you can something like this should work

```auto
OnPlayerRemoved(OutPlayer:player):void=
            if:
                OutPlayer.IsActive[]
                set YourWeakMap[OutPlayer] = player_stats_table{}

```

You cannot concatenate a weak map so removing a key will not work you can also not loop over a weak map but you can remove the data by just constructing a new class or data type and basically reset the stats and then set the player data to the new created class/data type. OutPlayer.IsActive is also important because if you don’t have it, it can throw a verse runtime error.

If you wanna store data between sessions and round check out this page [Using Persistable Data in Verse | Unreal Editor for Fortnite Documentation | Epic Developer Community](https://dev.epicgames.com/documentation/en-us/uefn/using-persistable-data-in-verse)

---

<div class="post-metadata">

**Author:** ![q\_e\_e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@q\_e\_e](https://forums.unrealengine.com/u/q_e_e)\
**Post date:** [July 28, 2024, 12:18pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/3 "2024-07-28T12:18:15Z")

</div>

I am sorry for the late answer, but thank you it did work, I have a question, when the player leaves it does delete it’s saved stats but how do i assign to him the current stats of the round?  
to put you in context; I am using this persistable as a score and the only thing i found on how to do it is using persistable data and i am using the player to track that score instead of the team. I want when the player who just joined to have the same score as the current players

---

<div class="post-metadata">

**Author:** ![Kasity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kasity](https://forums.unrealengine.com/u/Kasity)\
**Post date:** [July 29, 2024, 3:59pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/4 "2024-07-29T15:59:23Z")

</div>

Something you can do is grab the team collection then check what agents is on that team so if the new player should join team 1 you get fort\_playspace.GetTeamCollection().GetAgents[0] where 0 is team 1 and 1 is team 2 like a index in arrays. Then you loop over the array you get in return find a new agent(agent that isn’t the agent that just joined the team) convert them into a player player[Agent] and then get their score and set the new players score to that score you got 🙂

---

<div class="post-metadata">

**Author:** ![q\_e\_e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@q\_e\_e](https://forums.unrealengine.com/u/q_e_e)\
**Post date:** [July 29, 2024, 9:12pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/5 "2024-07-29T21:12:17Z")

</div>

Thank you for the idea it really helped it just had one flow in my case, if a player joins and there is no one on the team the script will probably won’t work so instead of getting the players on the same team i just grabbed all the existing players and then got their score, and I think it worked, I also noticed that when a game ends it doesnt count as players left the session and I have no idea if it actually reset the score or not but that is something I will see it later but anyway thank you for that idea 😁

---

<div class="post-metadata">

**Author:** ![WolfyGamingWorld](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfyGamingWorld](https://forums.unrealengine.com/u/WolfyGamingWorld)\
**Post date:** [August 14, 2024, 1:39pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/6 "2024-08-14T13:39:03Z")

</div>

I’ve been using this “solved” snippet as reference and I’ve tried many things something is alwaysa problem for my verse code it seems, and I can’t get it to work. Perhaps there is something here that is implied that I’m not understanding? I’ve been looking at the official pages " Using Persistable Data in Verse" and " Persistent Player Statistics" and I’m still not understanding.

---

<div class="post-metadata">

**Author:** ![q\_e\_e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@q\_e\_e](https://forums.unrealengine.com/u/q_e_e)\
**Post date:** [August 14, 2024, 4:10pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/7 "2024-08-14T16:10:09Z")

</div>

that snippet reset the weak map to its origianl value, all you have to do is copy and paste it as is and change YourWeakMap] to the name of your weak map and player\_stats\_table to the name of the class that have the default values

---

<div class="post-metadata">

**Author:** ![WolfyGamingWorld](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfyGamingWorld](https://forums.unrealengine.com/u/WolfyGamingWorld)\
**Post date:** [August 14, 2024, 4:14pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/8 "2024-08-14T16:14:45Z")

</div>

for (Key → Value : PlayerMap) {  
if (Key \<\> Agent) {  
set TempPlayerMap = ConcatenateMaps(TempPlayerMap, map{Key =\> Value})  
}  
}  
set PlayerMap = TempPlayerMap  
}  
}  
}‘’’

This is what I currently use how do I find the classes and name of the map?

---

<div class="post-metadata">

**Author:** ![q\_e\_e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@q\_e\_e](https://forums.unrealengine.com/u/q_e_e)\
**Post date:** [August 14, 2024, 4:20pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/9 "2024-08-14T16:20:49Z")

</div>

if you are using a normal map then this is how to do it

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/9/5/4958c01e4d7e3cc79797c20400fc7038a22566be.jpeg)

but if you are using a weak map like this one

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/8/0/b80ba04ff2a130943ddba83d3288b25ddb77e73c.png)

then the solution is that snippet

---

<div class="post-metadata">

**Author:** ![WolfyGamingWorld](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfyGamingWorld](https://forums.unrealengine.com/u/WolfyGamingWorld)\
**Post date:** [August 14, 2024, 7:44pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/11 "2024-08-14T19:44:07Z")

</div>

I’m using

‘’'vplayer\_data := class{  
StoredMoney : int = 0  
StoredPistol1 : int = 0  
StoredPistol2 : int = 0  
StoredPistol3 : int = 0  
StoredPistol4 : int = 0  
StoredPistol5 : int = 0  
StoredSmg1 : int = 0  
StoredSmg2 : int = 0  
StoredSmg3 : int = 0  
StoredSmg4 : int = 0  
StoredSmg5 : int = 0  
StoredShotgun1 : int = 0  
StoredShotgun2 : int = 0  
StoredShotgun3 : int = 0  
StoredShotgun4 : int = 0  
StoredShotgun5 : int = 0  
StoredSniper1 : int = 0  
StoredSniper2 : int = 0  
StoredSniper3 : int = 0  
StoredSniper4 : int = 0  
StoredRocketl1 : int = 0  
StoredRocketl2 : int = 0  
StoredRocketl3 : int = 0  
StoredRocketl4 : int = 0  
StoredAssaultr1 : int = 0  
StoredAssaultr2 : int = 0  
StoredAssaultr3 : int = 0  
StoredAssaultr4 : int = 0  
StoredAssaultr5 : int = 0  
StoredAssaultr6 : int = 0  
}

player\_data\_update(OldStat:player\_data) := player\_data{  
StoredMoney := OldStat.StoredMoney  
StoredPistol1 := OldStat.StoredPistol1  
StoredPistol2 := OldStat.StoredPistol2  
StoredPistol3 := OldStat.StoredPistol3  
StoredPistol4 := OldStat.StoredPistol4  
StoredPistol5 := OldStat.StoredPistol5  
StoredSmg1 := OldStat.StoredSmg1  
StoredSmg2 := OldStat.StoredSmg2  
StoredSmg3 := OldStat.StoredSmg3  
StoredSmg4 := OldStat.StoredSmg4  
StoredSmg5 := OldStat.StoredSmg5  
StoredShotgun1 := OldStat.StoredShotgun1  
StoredShotgun2 := OldStat.StoredShotgun2  
StoredShotgun3 := OldStat.StoredShotgun3  
StoredShotgun4 := OldStat.StoredShotgun4  
StoredShotgun5 := OldStat.StoredShotgun5  
StoredSniper1 := OldStat.StoredSniper1  
StoredSniper2 := OldStat.StoredSniper2  
StoredSniper3 := OldStat.StoredSniper3  
StoredSniper4 := OldStat.StoredSniper4  
StoredRocketl1 := OldStat.StoredRocketl1  
StoredRocketl2 := OldStat.StoredRocketl2  
StoredRocketl3 := OldStat.StoredRocketl3  
StoredRocketl4 := OldStat.StoredRocketl4  
StoredAssaultr1 := OldStat.StoredAssaultr1  
StoredAssaultr2 := OldStat.StoredAssaultr2  
StoredAssaultr3 := OldStat.StoredAssaultr3  
StoredAssaultr4 := OldStat.StoredAssaultr4  
StoredAssaultr5 := OldStat.StoredAssaultr5  
StoredAssaultr6 := OldStat.StoredAssaultr6  
}

var Data:weak\_map(player, player\_data) = map{}‘’‘’

so for player\_stats\_table its every last one of these?

---

<div class="post-metadata">

**Author:** ![WolfyGamingWorld](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfyGamingWorld](https://forums.unrealengine.com/u/WolfyGamingWorld)\
**Post date:** [August 14, 2024, 7:46pm UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/12 "2024-08-14T19:46:06Z")

</div>

> [@Kasity](#):
>
> `player_stats_table{}`

I guess what I’m not understanding is: What is the “player\_stats\_table{}” is it the list entirely ? like should I name or state it as a list as a whole collective category to make an array type of table? or is it each and every last one separately that is a table?

UPDATE: I think I’ve figured out that “'player\_data”’ is my table. I’m eager to try it, but fortnite servers are down apparently?

I use this:

‘’‘var Data:weak\_map(player, player\_data) = map{}’‘’’ so weak\_map is my “weakmap” and player\_data is my player\_stats\_table right?

Possibly the only thing I need help with now is what to put for ‘’‘[OutPlayer]’‘’’

This is where I’m at now my snippet looks like this:

‘’‘OnPlayerRemoved(OutPlayer:player):void=  
if:  
OutPlayer.IsActive  
set Data[OutPlayer] = player\_data{}’‘’

and the error I am getting is posted below:

An unrecoverable Verse runtime error occurred: Verse unrecoverable error: ErrRuntime\_WeakMapInvalidKey: Invalid key used to access persistent `var` `weak_map`.

Truncated callstack follows:  
task’ltimate?ice$UpdateValues(:player):Update (Unknown source) (Source: Unknown(0,0, 0,0))

---

<div class="post-metadata">

**Author:** ![WolfyGamingWorld](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfyGamingWorld](https://forums.unrealengine.com/u/WolfyGamingWorld)\
**Post date:** [August 17, 2024, 10:24am UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/13 "2024-08-17T10:24:07Z")

</div>

I have been returning back to this almost every hour to see if anyone has posted a real solution. I have spent approximately 100 hours on trying to fix this now and I’m starting to think everyone else knows that it plain can’t be done or something? I’m ready to give up I followed a few different YouTube tutorials and designed my code over on new maps a different way and it all ends in the same issue. I’ve posted on discord, this forum multiple times, reached out to old friends I have that do programming for a living and all has come up unsuccessful.

I’m going to move on and I guess try to duplicate my map and revert it back to where it was before I started using verse code because it just breaks your map.

---

<div class="post-metadata">

**Author:** ![Kasity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kasity](https://forums.unrealengine.com/u/Kasity)\
**Post date:** [September 1, 2024, 1:37am UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/14 "2024-09-01T01:37:59Z")

</div>

The error you getting is when the player you trying to access is not in the fort\_playspace. This is often fixed by checking if the player is active. So EVERYTIME you read or write to the Data variable you should check if the player you trying to access is active by just checking player.IsActive and if it fail you should not read or write to the Data variable. I don’t really know how your code looks but in this case the UpdateValues() function is the one crashing and not OnPlayerRemoved. So you probably write or read to the Data variable in the UpdateValues() function. So make sure you check if the player is active before reading or write to the Data variable.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [October 1, 2024, 1:38am UTC](https://forums.unrealengine.com/t/how-do-we-remove-player-stats-from-weak-map/1931475/15 "2024-10-01T01:38:28Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
