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 by User#24403 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
It's because you're doing it in a Script (serverside) and the server has no Player so the value is nil gullet 471 — 5y
0
local key = local key = game.Players:WaitForChild(player).UserId? SoulessKnowledge 2 — 5y

Answer this question