Here is the script I used to script my currency that saves.
01 | --Datastore service |
02 | local DSS = game:GetService( "DataStoreService" ) |
03 | local Money = DSS:GetDataStore( "Cash" ) |
04 |
05 |
06 | game.Players.PlayerAdded:Connect( function (plr) |
07 | local pCash = Money:GetAsync(plr.UserId) or 10 |
08 | local leaderstats = Instance.new( "Folder" ,plr) |
09 | leaderstats.Name = "leaderstats" |
10 | local CashValue = Instance.new( "NumberValue" ) |
11 | CashValue.Name = "Cash" |
12 | CashValue.Value = pCash |
13 | CashValue.Parent = leaderstats |
14 | CashValue.Changed:connect( function (v) |
15 | Money:SetAsync(plr.UserId,v) |
16 | print ( "MoneySaved!" ) |
17 | end ) |
18 | end ) |
your code is very unclear i have made a better version for you.
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local Money = DataStoreService:GetDataStore( "Cash" ) |
03 |
04 | function playeradded(plr) |
05 | local leaderstats = Instance.new( "Folder" ) |
06 | leaderstats.Parent = plr |
07 | leaderstats.Name = "leaderstats" |
08 |
09 | local Cash = Instance.new( "NumberValue" ) |
10 | Cash.Name = "Cash" |
11 | Cash.Value = Money:GetAsync(plr.UserId) or 10 |
12 | Cash.Parent = leaderstats |
13 | end |
14 |
15 | function plrremoving(plr) |
16 | Money:SetAsync(plr.UserId, plr.leaderstats.Cash.Value) |
17 | end |
18 |
19 | game.Players.PlayerAdded:Connect(playeradded) |
20 | game.Players.PlayerRemoving:Connect(plrremoving) |
If this still don't work check if you have Api acces via studio enabled.