Why does my data saving script index local 'player' as a nil value?
Asked by
6 years ago Edited 6 years ago
I have a saving script that saved my data the first time but would not the next times. So I can tell that the problem is overwriting data. I even put in a anew thing to save and it saved it once but would not the next time. Thanks in Advance. Also someone on my last one said,"because the player is in the players service , not workspace" can someone tell me if that is correct and if so tell me what they mean by that.
Here is my Script I put into "ServerScriptServicee" I have another script that creates everything being saved in this script
01 | local Players = game:GetService( "Players" ) |
02 | local DataStoreService = game:GetService( "DataStoreService" ) |
03 | local DataStore = DataStoreService:GetDataStore( "-xBuckDatax-" ) |
06 | local function Save(player) |
07 | local key = local key = game.Players:WaitForChild(player).UserId |
08 | local plr = workspace:WaitForChild(player.Name) |
11 | [ "SeerBux" ] = player.leaderstats [ "SeerBux" ] .Value, |
12 | [ "SpdWaitTime" ] = player.WaitTime.WaitTimeSpd.Value, |
13 | [ "StrWaitTime" ] = player.WaitTime.WaitTimeStr.Value, |
14 | [ "walkSpeed" ] = plr:WaitForChild( 'Humanoid' ).WalkSpeed, |
15 | [ "customize" ] = player.Customize.Value, |
16 | [ "Exp" ] = player.leaderstats.ExperiencePts.Value, |
17 | [ "level" ] = player.leaderstats.Level.Value, |
18 | [ "SpdCheck" ] = player.SpdCheck.Value, |
19 | [ 'energy' ] = player.EnergyCount.Value, |
20 | [ 'enbool' ] = player.EnergyBool.Value |
24 | local success, err = pcall ( function () |
25 | DataStore:SetAsync(key, save) |
29 | warn( "Failed to over-write data" .. tostring (err)) |
35 | local function Load(player) |
37 | local key = player.UserId |
38 | local plr = workspace:WaitForChild(player.Name) |
42 | local success, err = pcall ( function () |
43 | moneyAlready = DataStore:GetAsync(key) |
47 | warn( "Failed to read data" .. tostring (err)) |
52 | player.leaderstats.SeerBux.Value = moneyAlready.SeerBux |
53 | player.WaitTime.WaitTimeSpd.Value = moneyAlready.SpdWaitTime |
54 | player.WaitTime.WaitTimeStr.Value = moneyAlready.StrWaitTime |
55 | plr:WaitForChild( 'Humanoid' ).WalkSpeed = moneyAlready.walkSpeed |
56 | player.Customize.Value = moneyAlready.customize |
57 | player.leaderstats.ExperiencePts.Value = moneyAlready.ExperiencePts |
58 | player.leaderstats.Level.Value = moneyAlready.Level |
59 | player.SpdCheck.Value = moneyAlready.SpdCheck |
60 | player.EnergyCount.Value = moneyAlready.energy |
61 | player.EnergyBool.Value = moneyAlready.enbool |
69 | Players.PlayerAdded:Connect(Load) |
70 | Players.PlayerRemoving:Connect(Save) |
Here's the leaderboard script in case you wanted it. Not everythin here is saved. I just use this script to create Values, animations, etc. in game.
01 | game.Players.PlayerAdded:Connect( function (player) |
02 | player.CharacterAdded:Connect( function (char) |
03 | local humanoid = char:WaitForChild( "Humanoid" ) |
04 | local animate = char:WaitForChild( "Animate" ) |
05 | local ls = Instance.new( "Folder" , player) |
06 | ls.Name = "leaderstats" |
07 | local stats = Instance.new( "Folder" , player) |
09 | local Cash = Instance.new( "IntValue" , ls) |
12 | local level = Instance.new( "IntValue" , ls) |
15 | local Custom = Instance.new( "BoolValue" , player) |
16 | Custom.Name = "Customize" |
18 | local waittimeFold = Instance.new( "Folder" , player) |
19 | waittimeFold.Name = 'WaitTime' |
20 | local waittimespd = Instance.new( "IntValue" , waittimeFold) |
21 | waittimespd.Name = 'WaitTimeSpd' |
23 | local exp = Instance.new( "IntValue" , ls) |
24 | exp.Name = "ExperiencePts" |
26 | local waittimeStr = Instance.new( "IntValue" , waittimeFold) |
27 | waittimeStr.Name = 'WaitTimeStr' |
29 | local power = Instance.new( "IntValue" , stats) |
32 | local energy = Instance.new( 'IntValue' , player) |
33 | energy.Name = 'EnergyCount' |
35 | local boolenergy = Instance.new( 'BoolValue' , player) |
36 | boolenergy.Value = true |
37 | boolenergy.Name = 'EnergyBool' |
38 | local usebool = Instance.new( 'BoolValue' , player) |
39 | usebool.Name = 'EnergyUsed' |
41 | local lift = Instance.new( 'StringValue' , workspace.Animations) |
43 | local liftanim = Instance.new( 'Animation' , lift) |
44 | liftanim.Name = 'LiftAnim' |