Hello!
Have you tried putting using the "PlayerRemoved" event outside of the PlayerAdded event?
Also, your keys are different. You used different keys to save when leaving, and when the value changes.
If you want to grab a key that looks like this:
CashStore:GetAsync("Points_"..player.UserID)
Then save a key that looks the same:
CashStore:SetAsync("Points_"..player.UserID, cash.Value)
If you don't know what I mean, it should look something like this.
01 | local CashStore = game:GetService( "DataStoreService" ):GetDataStore( "DataStore" ) |
03 | function PlayerEntered(player) |
04 | repeat wait() until player.Character |
06 | local stats = Instance.new( "IntValue" ) |
08 | stats.Name = "leaderstats" |
10 | local cash = Instance.new( "IntValue" ) |
12 | cash.Name = "Doubloons" |
14 | if CashStore:GetAsync( "Points_" ..player.UserID) ~ = nil then |
15 | cash.Value = CashStore:GetAsync( "Points_" ..player.UserID) |
20 | cash.Changed:connect( function (Val) |
21 | CashStore:SetAsync( "Points_" ..player.UserID, Val) |
25 | function PlayerLeft(player) |
26 | CashStore:SetAsync( "Points_" ..player.UserID, cash.Value) |
29 | game.Players.PlayerRemoved:connect(PlayerLeft) |
30 | game.Players.PlayerAdded:connect(PlayerEntered) |
Apologies if I made any mistakes, I wrote this with a burnt hand.