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 = 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)

11 comments

0

the person got confused on line 38 because you named the character variable as 'plr'

0

I suggest to make the leaderboard script fire the 'Load()' function to prevent any "nil" errors.

0

does any errors pop up? comment it

0

I may be mistaken, but I think that a BindToClose ( https://developer.roblox.com/api-reference/function/DataModel/BindToClose ) may be needed to allow the server sufficient time to save data prior to the server shutting down.

0

Ok thx. BTW - There are no errors on the output, I wish the output could tell me though

0

The save script always tuns after the leaderboard so i should be good on that but i'll look into BindToClose

0

Ok I added BindToClose. The article said something about making SetAsync yields but I don't know how to do that. If that is necessary can someone please tell me how to do so

0

that wouldn't work it will just error saying that 'player' is a nil value (for the bindtoclose)

0

What do i need to do

0

I'll be back in 10 min. to check for any comments

0

I'm going to sleep now. I have school tomorrow and probably can't check this until 3:50 p.m. Eastern time tomorrow