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

My DataStore Is Not Working To Save Players Leaderstats Any Way To Fix This?

Asked by 1 year ago

So im having trouble with saving players leaderstats and im kind of new to datastores and note I was watching thedevkings video so idk what I did wrong.

local plr = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local MyData = DSS:GetDataStore("MyData1") -- gets a data store and you can name it
--- plr name
plr.PlayerAdded:Connect(function(player)
    print(player.Name)
    local text = game.StarterGui.ScreenGui.TextLabel
    text.Text = player.Name
    --- leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 100
    while true  do
        task.wait(5)
        Coins.Value = Coins.Value + 1
    end
    -- datastore
    local PlrId = "Player_"..player.UserId
-- syncs player data from datastore GetAsync()
    local data1
    local success, errormessage = pcall(function() -- pcall stops errors
       data1 = MyData:GetAsync(PlrId) 
    end)
    if success then
        -- set our data to our coin value
        Coins.Value = data1
    end
    plr.PlayerRemoving:Connect(function(player)
        local PlrId = "Player_"..player.UserId
        local plrdata = plr:WaitForChild(leaderstats)
        local success, errormessage = pcall(function()
            MyData:SetAsync(PlrId, plrdata)
        end)
        if success then
            print("SUCCESS")
        else
            print("not good")
            warn(errormessage)
        end
    end)
end)

1
Just wanted to add that the `while true do` loop in your script completely stops the rest of the script from executing. DindinYT37 246 — 1y

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

There's a lot of problems in your script that I'm too lazy to list all. Here's the script, just observe this script and the previous one to learn your mistakes.

local plr = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local MyData = DSS:GetDataStore("MyData1") -- gets a data store and you can name it
local RunService = game:GetService("RunService")

local function waitForRequestBudget(requestType: Enum.DataStoreRequestType)
    local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)

    while currentBudget < 1 do
        currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
        task.wait(5)
    end
end

function loadData(player)
    print(player.Name)
    local text = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").TextLabel
    text.Text = player.Name
    --- leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 100
    -- datastore
    local PlrId = "Player_"..player.UserId
    -- syncs player data from datastore GetAsync()
    wait
    waitForRequestBudgetType(Enum.DataStoreRequestType.GetAsync)
    local success, data1 = pcall(function() -- pcall stops errors
        return MyData:GetAsync(PlrId) 
    end)
    if success then
        -- set our data to our coin value
        Coins.Value = data1
    else
        warn(data1)
    end

    task.spawn(function()
        while true do
            task.wait(5)
            Coins.Value += 1
        end
    end)
end

function saveData(player, dontWait)
    local PlrId = "Player_"..player.UserId
    local plrdata = plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value
    local success
    repeat
        if not dontWait then
            waitForRequestBudgetType(Enum.DataStoreRequestType.SetAsync)
        end
        success = pcall(function()
            MyData:SetAsync(PlrId, plrdata)
        end)
    until success
end

for _, player in ipairs(plr:GetPlayers()) do
    task.spawn(loadData, player)
end

plr.PlayerAdded:Connect(loadData)
plr.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
    if RunService:IsStudio() then
        task.wait(2)
    else
        local finished = Instance.new("BindableEvent")
        local allPlayers = Players:GetPlayers()
        local leftPlayers = #allPlayers

        for _,player in ipairs(allPlayers) do
            task.spawn(function()
                saveData(player, true)
                leftPlayers -= 1
                if leftPlayers == 0 then
                    finished:Fire()
                end
            end)
        end

        finished.Event:Wait()
    end
end)
Ad

Answer this question