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

Nothing is being printed out when a player leaves?

Asked by 5 years ago

Also, if I don't have the spawn(function() it will just take forever for the game to close (on studio) and save the players data. And once it has finally closed it would say "Not running script because past shutdown deadline (x247)"

local DSS = game:GetService("DataStoreService"):GetDataStore("Testing")

local DefaultStats = {
    Coins = 0,
    Blocks = 0,
    Rebirths = 0,
}

local function load(player)
    local key = "user_"..player.UserId  

    local success, data  = pcall(function()
        return DSS:GetAsync(key)
    end)    

    if(success)then
        print("Loaded "..player.Name.."'s Data!")

        if data == nil then
            data = DefaultStats         
        end

        local leaderstats = Instance.new("Folder",player)
        leaderstats.Name = "leaderstats"

        local Coins = Instance.new("IntValue",leaderstats)
        Coins.Name = "Coins"

        local Blocks = Instance.new("IntValue",leaderstats)
        Blocks.Name = "Blocks"

        local Rebirths = Instance.new("IntValue",leaderstats)
        Rebirths.Name = "Rebirths"

        Coins.Value = data.Coins
        Blocks.Value = data.Blocks
        Rebirths.Value = data.Rebirths

        local CurrentRocket = Instance.new("StringValue",player)
        CurrentRocket.Name = "Current_Rocket"
        CurrentRocket.Value = "Rocket"
    else
        print("Couldn't Load "..player.Name.."'s Data!")
    end
end

local function save(player)
    local key = "user_"..player.UserId

    local Items = {
        Coins = player.leaderstats.Coins.Value,
        Blocks = player.leaderstats.Blocks.Value,
        Rebirths = player.leaderstats.Rebirths.Value,
    }

    local Success, message = pcall(function()
        DSS:SetAsync(key,Items)
    end)

    if Success then
        print("Saved "..player.Name.."'s Stats")
    else
        print("Failed to save "..player.Name.. "'s Stats")
    end

end

game:BindToClose(function()
    for _, player in ipairs(game.Players:GetPlayers()) do
        spawn(function()
            save(player)
        end)
    end
end)

game.Players.PlayerAdded:Connect(function(player) 
    load(player) 
end)

game.Players.PlayerRemoving:Connect(function(player) 
    save(player)
end)
0
Does it show on the output in errors? fortesss7 40 — 5y
0
DataStores are pretty tricky. Did you test it on a 2-player server? Tymberlejk 143 — 5y
0
The only errors it shows are that the data had been on a queue and that the script had passed shutdown deadline. I tried this in 2 player it says it saves but then it makes another datastore script not work. Raslito 7 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

you need to only allow the server to shutdown when data has been saved

0
So then.. what are you suggesting I do to the script? :/ Raslito 7 — 5y
Ad

Answer this question