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

Data not loading?! (It works fine besides it not loading)

Asked by 4 years ago
Edited 4 years ago

The data script below works all well but refuses to load. It doesn't show any errors in output! Any idea whats going on? lmk thx


The issue is that the data wont load correctly! pls help

local data = require(script.DataModule)

local DSS = game:GetService("DataStoreService")
local statSave = DSS:GetDataStore("stats_Test2020")
local settingSave = DSS:GetDataStore("settings_Test2020")
local careerSave = DSS:GetDataStore("careers_Test2020")

--ughh its not working

game.Players.PlayerAdded:Connect(function(player)
    local key = "Data-ID"..player.UserId

    local statData = statSave:GetAsync(key)
    local settingData = settingSave:GetAsync(key)
    local careerData = careerSave:GetAsync(key)

    local StatsFolder = Instance.new("Folder", player)
    local SettingsFolder = Instance.new("Folder", player)
    local CareersFolder = Instance.new("Folder", player)

    StatsFolder.Name = "leaderstats"
    SettingsFolder.Name = "Settings"
    CareersFolder.Name = "Careers"

    -----

    for i,stats in pairs(data["Stats"]) do
        local thing = Instance.new("IntValue", StatsFolder) 
        thing.Name = tostring(stats)
        thing.Value = 0
    end
    for i,setting in pairs(data["Settings"]) do
        local thing = Instance.new("BoolValue", SettingsFolder)
        thing.Name = tostring(setting)
    end
    for i,career in pairs(data["Careers"]) do
        local thing = Instance.new("BoolValue", CareersFolder)
        thing.Name = tostring(career)
    end

    if statData then
        for i,v in pairs(StatsFolder:GetChildren()) do
            v.Value = statData[i]
        end
    else
        local StatTable = {}

        for i,v in pairs(StatsFolder:GetChildren()) do
            table.insert(StatTable, v.Value)
        end

        statSave:SetAsync(key, StatTable)
    end

    if settingData then
        for i,v in pairs(SettingsFolder:GetChildren()) do
            v.Value = settingData[i]
        end
    else
        local SettingsTable = {}

        for i,v in pairs(SettingsFolder:GetChildren()) do
            table.insert(SettingsTable, v.Value)
        end

        settingSave:SetAsync(key, SettingsTable)
    end

    if careerData then
        for i,v in pairs(CareersFolder:GetChildren()) do
            v.Value = careerData[i]
        end
    else
        local CareersTable = {}

        for i,v in pairs(CareersFolder:GetChildren()) do
            table.insert(CareersTable, v.Value)
        end

        settingSave:SetAsync(key, CareersTable)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local key = "Data-ID"..player.UserId
    local Stats = player.leaderstats
    local Settings = player.Settings
    local Careers = player.Careers

    local statsTable = {}
    local settingsTable = {}
    local careersTable = {}

    for i,v in pairs(Stats:GetChildren()) do
        table.insert(statsTable, v.Value)
    end
    for i,v in pairs(Settings:GetChildren()) do
        table.insert(settingsTable, v.Value)
    end
    for i,v in pairs(Careers:GetChildren()) do
        table.insert(careersTable, v.Value)
    end

    statSave:SetAsync(key, statsTable)
    settingSave:SetAsync(key, settingsTable)
    careerSave:SetAsync(key, careersTable)
end)
0
i had trouble like this before and how i solved it was just adding pcalls HappyTimIsHim 652 — 4y
0
it seems like ur making int and boolvalues looping through the saved data. if there's nothing inside then it wouldn't create anything i guess HappyTimIsHim 652 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I just skimmed through your script, but you said everything was working fine except for the data not saving. I know a lot of people have this problem too. Check out this article. Hopefully this answers your question. Basically, the server may shut down before the last player leaves or if you're playing Solo in the Studio, thus not being able to complete all the functions. To fix that, use BindToClose. Place the function that you want to run before the server shuts down, inside of the BindToClose function. The function should be completed less than 30 seconds at most before the server shuts down.

game:BindToClose(function() -- Runs the PlayerRemoving function before server shuts down
    for i, player in pairs(game.Players:GetPlayers()) do

    local key = "Data-ID"..player.UserId
    local Stats = player.leaderstats
    local Settings = player.Settings
    local Careers = player.Careers

    local statsTable = {}
    local settingsTable = {}
    local careersTable = {}

    for i,v in pairs(Stats:GetChildren()) do
        table.insert(statsTable, v.Value)
    end
    for i,v in pairs(Settings:GetChildren()) do
        table.insert(settingsTable, v.Value)
    end
    for i,v in pairs(Careers:GetChildren()) do
        table.insert(careersTable, v.Value)
    end

    statSave:SetAsync(key, statsTable)
    settingSave:SetAsync(key, settingsTable)
    careerSave:SetAsync(key, careersTable)
end
end)
0
tysm ill try this iiDev_Hunt3r 64 — 4y
0
Its still not working xD iiDev_Hunt3r 64 — 4y
0
Make sure that you did not delete the PlayerRemoving function. Simply keep your original script, but then add this one at the end. AntiWorldliness 868 — 4y
0
It works now xD But datas being throttled iiDev_Hunt3r 64 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I had this problem for a while and i figured Values inside of player when updated don't update for the server(or something like that idk). the way i worked around this was i had a remote send the data from a local script to the server when it was called.

example...

Client:

local remote = <remote>

local data = <value thingy in player>.Value

local function save()
    remote:FireServer(data)
end

Server:

local remote = <remote>

remote.OnServerEvent:Connect(function(player, data)
    Datastore:SetAsync(key, data)
end)

idk but it works

0
i- i know that xD i am using a remote... iiDev_Hunt3r 64 — 4y
0
The refuses to load/save iiDev_Hunt3r 64 — 4y
0
? i see not a remote kie1605 0 — 4y
0
You dont need a remote for data... you need it to change stats but im trying to load data! Who uses remotes to load data xD iiDev_Hunt3r 64 — 4y

Answer this question