Data Store Error [plz help?] [Repost]
Asked by
4 years ago Edited 4 years ago
HELP its been about month my game is delayed cuz the datastore error
link to original Post: https://scriptinghelpers.org/questions/105089/data-store-error-plz-help
Can somebody fix my code to work, im trying to make a DataStore for Cash
Output: DS isn't a vaild part of DataModel
Thanks to anybody who fixes it!
01 | local DS = game:GetService( "DataStoreService" ) |
02 | local CurrencySave = game.DS:GetService( "FirstStore" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
07 | local leaderstats = Instance.new( "Folder" , player) |
08 | leaderstats.Name = "leaderstats" |
09 | leaderstats.Parent = player |
11 | local cash = Instance.new( "IntValue" , leaderstats) |
16 | local success, errormessage = pcall ( function (player) |
17 | data:GetAsync(player.UserId.. "-cash" ) |
23 | print ( "There was an error while getting your data" ) |
28 | game.Players.PlayerRemoving:Connect( function (player) |
30 | local success, errormessage = pcall ( function () |
31 | CurrencySave:SetAsync(player.UserId.. "-cash" , player.leaderstats.cash.Value) |
35 | print ( "Player data Saved!" ) |
37 | print ( "Player Data didn't Save." ) |
Code im currently using
Output: attempt to index nil with 'UserId'
01 | local DS = game:GetService( "DataStoreService" ) |
02 | local CurrencySave = DS:GetDataStore( "FirstStore" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
07 | local leaderstats = Instance.new( "Folder" , player) |
08 | leaderstats.Name = "leaderstats" |
09 | leaderstats.Parent = player |
11 | local cash = Instance.new( "IntValue" , leaderstats) |
13 | cash.Parent = leaderstats |
17 | local success, errormessage = pcall ( function (player) |
18 | data:GetAsync(player.UserId.. "-cash" ) |
24 | print ( "There was an error while getting your data" ) |
29 | game.Players.PlayerRemoving:Connect( function (player) |
31 | game:BindToClose( function () |
32 | for _, v in pairs (game.Players:GetPlayers()) do |
33 | local success, errorMessage = pcall ( function () |
34 | CurrencySave:SetAsync(v.UserId,v.leaderstats.cash.Value) |
37 | print ( "Data saved and game closed" ) |
39 | warn( "Error while saving data: " ..errorMessage) |
44 | local success, errormessage = pcall ( function () |
45 | CurrencySave:SetAsync(player.UserId.. "-cash" , player.leaderstats.cash.Value) |
49 | print ( "Player data Saved!" ) |
51 | print ( "Player Data didn't Save." ) |
the output is the same but here,
Output: attempt to index nil with 'UserId'
but once i click on the error it brings me to line 25,
warn(errormessage)