How to make my Datastore load the player's Cash?
Script so when the player joins the game, it gives the leaderstats folder + the Cash value inside
01 | function onPlayerEntered(newPlayer) |
03 | local stats = Instance.new( "Folder" ) |
04 | stats.Name = "leaderstats" |
06 | local score = Instance.new( "IntValue" ) |
12 | stats.Parent = newPlayer |
15 | game.Players.ChildAdded:connect(onPlayerEntered) |
Datastore Script
01 | local ds = game:GetService( "DataStoreService" ):GetDataStore( "CashData" ) |
03 | game.Players.PlayerAdded:Connect( function (plr) |
07 | local plrkey = "id_" ..plr.userId |
08 | local leaderstats = plr:WaitForChild( "leaderstats" ) |
09 | local cash = leaderstats:WaitForChild( "Cash" ) |
11 | local GetSaved = ds:GetAsync(plrkey) |
14 | cash.Value = GetSaved [ 1 ] |
16 | local NumberForSaving = { cash.Value } |
17 | ds:GetAsync(plrkey, NumberForSaving) |
21 | ds:SetAsync( "id_" ..plr.userId, { plr.leaderstats.Cash.Value } ) |
25 | game.Players.PlayerRemoving:Connect( function (plr) |
26 | local leaderstats = plr:WaitForChild( "leaderstats" ) |
27 | local cash = leaderstats:WaitForChild( "Cash" ) |
28 | ds:SetAsync( "id_" ..plr.userId, { cash.Value } ) |
[Error is from the Datastore script]:
"ServerScriptService.CashDatastore:14: attempt to index number with number"