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
5 years ago
Edited 5 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

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("-xBuckDatax-")

-- This function saves the players data
local function Save(player)
    local key = local key = game.Players:WaitForChild(player).UserId
    local plr = workspace:WaitForChild(player.Name)

    local save = {
        ["SeerBux"] = player.leaderstats["SeerBux"].Value,
        ["SpdWaitTime"] = player.WaitTime.WaitTimeSpd.Value,
        ["StrWaitTime"] = player.WaitTime.WaitTimeStr.Value,
        ["walkSpeed"] = plr:WaitForChild('Humanoid').WalkSpeed,
        ["customize"] = player.Customize.Value,
        ["Exp"] = player.leaderstats.ExperiencePts.Value,
        ["level"] = player.leaderstats.Level.Value,
        ["SpdCheck"] = player.SpdCheck.Value,
        ['energy'] = player.EnergyCount.Value,
        ['enbool'] = player.EnergyBool.Value
    }

    --saves their key and money to the data store
    local success, err = pcall(function()
        DataStore:SetAsync(key, save)
    end)

    if not success then
        warn("Failed to over-write data"..tostring(err))
        return
    end
end

-- This function loads the players data
local function Load(player)
    -- saves the player Id as key
    local key = player.UserId
    local plr = workspace:WaitForChild(player.Name)

    local moneyAlready

    local success, err = pcall(function()
        moneyAlready = DataStore:GetAsync(key)
    end)

    if not success then
        warn("Failed to read data"..tostring(err))
        return
    end

    if moneyAlready then
        player.leaderstats.SeerBux.Value = moneyAlready.SeerBux
        player.WaitTime.WaitTimeSpd.Value = moneyAlready.SpdWaitTime
        player.WaitTime.WaitTimeStr.Value = moneyAlready.StrWaitTime
        plr:WaitForChild('Humanoid').WalkSpeed = moneyAlready.walkSpeed
        player.Customize.Value = moneyAlready.customize
        player.leaderstats.ExperiencePts.Value = moneyAlready.ExperiencePts
        player.leaderstats.Level.Value = moneyAlready.Level
        player.SpdCheck.Value = moneyAlready.SpdCheck
        player.EnergyCount.Value = moneyAlready.energy
        player.EnergyBool.Value = moneyAlready.enbool

    else
        Save(player)
    end
end

game:BindToClose(Save)
Players.PlayerAdded:Connect(Load)
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.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
        local animate = char:WaitForChild("Animate")
        local ls = Instance.new("Folder", player)
        ls.Name = "leaderstats"
        local stats = Instance.new("Folder", player)
        stats.Name = 'Stats'
        local Cash  =Instance.new("IntValue", ls)
        Cash.Value = 1000
        Cash.Name = "SeerBux"
        local level = Instance.new("IntValue", ls)
        level.Name = "Level"
        level.Value = 1
        local Custom = Instance.new("BoolValue", player)
        Custom.Name = "Customize"
        Custom.Value = false
        local waittimeFold = Instance.new("Folder", player)
        waittimeFold.Name = 'WaitTime'
        local waittimespd = Instance.new("IntValue", waittimeFold)
        waittimespd.Name = 'WaitTimeSpd'
        waittimespd.Value = 3
        local exp = Instance.new("IntValue", ls)
        exp.Name = "ExperiencePts"
        exp.Value = 0
        local waittimeStr = Instance.new("IntValue", waittimeFold)
        waittimeStr.Name = 'WaitTimeStr'
        waittimeStr.Value = 3
        local power = Instance.new("IntValue", stats)
        power.Name = "Power"
        power.Value = 5
        local energy = Instance.new('IntValue', player)
        energy.Name = 'EnergyCount'
        energy.Value = 22
        local boolenergy = Instance.new('BoolValue', player)
        boolenergy.Value = true
        boolenergy.Name = 'EnergyBool'
        local usebool = Instance.new('BoolValue', player)
        usebool.Name = 'EnergyUsed'
        usebool.Value = false
        local lift = Instance.new('StringValue', workspace.Animations)
        lift.Name = 'Lift'
        local liftanim = Instance.new('Animation', lift)
        liftanim.Name = 'LiftAnim'
        liftanim.AnimationId = 'http://www.roblox.com/asset/?id=2914392738'
    end)
end)

0
Sorry for the late response, it's because you reiterated the keyword 'local'. Ziffixture 6913 — 5y

3 answers

Log in to vote
0
Answered by 5 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 — 5y
0
I wasn't getting sassy, sorry if I sounded that way sheepposu 561 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Was testing your code myself, found these.

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

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

        local exp = Instance.new("IntValue", ls)
        exp.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 — 5y
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 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 — 5y
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 — 5y

Answer this question