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

The function to save my data doesn't work, help?

Asked by 2 years ago
Edited 2 years ago

This piece of code from a big block seems to be the problem. It doesn't print out "Save 2"

local function SaveData(player)
    if player.userId < 0 then return end
    player:WaitForChild("leaderstats")
    wait()
    local leaderstats = {}
    for i, stat in pairs(player.leaderstats:GetChildren()) do
        if not stat:IsA("Folder") then
            print("Save 1")
            table.insert(leaderstats, {stat.Name, stat.Value})
        end
    end
    player:WaitForChild("leaderstats"):WaitForChild("Rebirth")
    wait()
    for i, stat in pairs(player.leaderstats.Rebirths:GetChildren()) do
        print("Save 2")
        table.insert(leaderstats, {stat.Name, stat.Value})
    end
    leaderboardData:SetAsync(player.userId, leaderstats)
    Print("Saved "..player.Name.."'s data")
end

leaderstats folder: https://drive.google.com/file/d/1j1v2ZnDaCvP1ifK-H1XgtWIJffGFppzW/view?usp=sharing

0
I can't see the file. 'Access denied' AProgrammR 398 — 2y
0
oh GameStealerKid 79 — 2y
0
it should work now GameStealerKid 79 — 2y
0
Is there any errors? it seems that it should work. AProgrammR 398 — 2y
View all comments (4 more)
0
no errors GameStealerKid 79 — 2y
0
but it doesnt work GameStealerKid 79 — 2y
0
did you run the function? can you give the script where you run it? AProgrammR 398 — 2y
0
oh, and enable studio acces to api service in game settings. Did you enable it yet? AProgrammR 398 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

I just got confused with the value "Rebirths" and the folder "Rebirth" sry everybody

0
i see, how did i not notice that, lol. AProgrammR 398 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

in for i,v (save2) you have leaderstats.Rebirths but before for i,v you have WaitForChild("Rebirth") and not Rebirths

or

use this script and put all in 1 folder

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game:GetService("Players").PlayerAdded:Connect(function(player)
    local playerUserId = "Player_"..player.UserId
    local data = playerData:GetAsync(playerUserId)

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

    local XP = Instance.new("NumberValue", Folder)
    XP.Name = "XP" -- this is example

    local LVL = Instance.new("NumberValue", Folder)
    LVL.Name = "Levels"

    local XPN = Instance.new("BoolValue", Folder)
    XPN.Name = "XPN"

    if data then
        XP.Value = data["XP"]
        LVL.Value = data["Levels"]
        XPN.Value = data["XPN"]
    else
        XP.Value = 0
        LVL.Value = 0
        XPN.Value = true
    end
end)

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

game:GetService("Players").PlayerRemoving:Connect(function(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        playerData:SetAsync(playerUserId, player_stats)
    end)
    if not success then
        warn("Could not save data")
    end

end)

Answer this question