Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my data saving script index local 'player' as a nil value?

Asked by
sheepposu 561 Moderation Voter
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

01local Players = game:GetService("Players")
02local DataStoreService = game:GetService("DataStoreService")
03local DataStore = DataStoreService:GetDataStore("-xBuckDatax-")
04 
05-- This function saves the players data
06local function Save(player)
07    local key = local key = game.Players:WaitForChild(player).UserId
08    local plr = workspace:WaitForChild(player.Name)
09 
10    local save = {
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,
View all 70 lines...

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.

01game.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)
08        stats.Name = 'Stats'
09        local Cash  =Instance.new("IntValue", ls)
10        Cash.Value = 1000
11        Cash.Name = "SeerBux"
12        local level = Instance.new("IntValue", ls)
13        level.Name = "Level"
14        level.Value = 1
15        local Custom = Instance.new("BoolValue", player)
View all 47 lines...
0
Sorry for the late response, it's because you reiterated the keyword 'local'. Ziffixture 6913 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago

I got two spots that might be it:

line 40: local moneyAlready :Where is what it equals?

line 3: Invalid datastore? (-xBuckDatax-)

0
Line 40 is not a problem 1) I only made a variable, just didn't give it a value or anything. It's okay since i gave it a value before using it. 2) If invalid datastore were the case it wouldn't have saved the first time. I never changed the name of the data store so that's definitely not the problem sheepposu 561 — 6y
0
I wasn't getting sassy, sorry if I sounded that way sheepposu 561 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Was testing your code myself, found these.

1["Exp"] = player.ExperiencePts.Value,

In your load/save script and then in your leaderstats,

1local exp = Instance.new("IntValue", ls)
2exp.Name = "ExperiencePts"

the leaderstats defining experiencePts as being a child of leaderstats, while in the load you are caling it as directly under player. Not sure if this is the only issue, but this is one. Which would be stopping much of your code from running properly.

You should also check the other values to see if any of them are affected by the same issue.

0
thanks, that will help me save my Exp but it doesn't solve the overwriting problem sheepposu 561 — 6y
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

It means you've apparently attempted to reference the workspace, though instead referenced the Players Service; indexed 'Players'.


I believe what the person was trying to inform you about was getting the Character. Don't attempt to find it by looking for it in workspace, if there is another Instance with an identical handle, it can potentially return an unanticipated Object.

Simply write: local Character = Player.Character or Player.CharacterAdded:Wait() *for a more accurate and direct route.


Though I believe this isn't the case and that you were actually trying to reference the PlayerInstance. In this case, the person that informed you was correct–PlayersInstances reside in the Players Service.

To properly Index the PlayerInstance, you need to reference it as a Child of said Service, this can simply be accomplished by writing Players.LocalPlayer–to retrieve the local ClientPlayers:GetPlayerFromCharacter(instance Character)–preform a reverse search–and Players[Instance.Name]; however this method specifically is more case sensitive.

0
Also, try using :UpdateAsync too? Ziffixture 6913 — 6y
0
Ok. The problem is line 7 where I update their key. I changed it to "key = game.Players:WaitForChild(player).UserId" to see if that would reference specifically to the player, but it gave me the error of missing an arg. Would doing this help the problem or is it most likely going to lead me to a dead end sheepposu 561 — 6y

Answer this question