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

Datastore not working, for the third time?

Asked by 3 years ago

Btw, before you flag this, im not spamming. I just need help. So i rewrote the script and added bind to close. And it prints saved successfully, but when i rejoin on studio, it doesn't load up. I've tried everything, Allowing studio to get API Access, making it public.... Adding bind to close.. NOTHING works... As far as i can tell, nothing is wrong with the script... Here is the script:

local datastoreservicee = game:GetService("DataStoreService") --- Gets Datastore Service
local AbiltiyStore = datastoreservicee:GetDataStore("AbilityStore") --Get's Ability Store


game.Players.PlayerAdded:Connect(function(player) -- Defining the player added
    local leaderstats = Instance.new("Folder") -- Creates A folder
    leaderstats.Name = "leaderstats" --- Names folder 'leaderstats'
    leaderstats.Parent = player -- parents folder to player --

    local Ability = Instance.new("StringValue") --- Makes a new string value which can store numbers and letters
    Ability.Name = "Ability" --- changes string value name to Ability
    Ability.Parent = leaderstats -- Puts it in the folder leaderstats

    local playeruserid = "Player_"..player.UserId -- Makes A playeruser id variable


    local data -- Defining the variable, will store something in it later

    local success, errormessage = pcall(function() --Uses this to search for errors during data saving
        data = AbiltiyStore:GetAsync(playeruserid) 
    end)
    if success then
        Ability.Value = data[1]
        print("Set Data Successfully")
    end
end)

--New function, when player leaves...
game.Players.PlayerRemoving:Connect(function(player) -- When player leaves...
    local Playeruserid = "Player_"..player.UserId -- Redifining the variable, because the variable before was local
     local data = {player.leaderstats.Ability.Value} -- Creates a table, so we can store numerous stuff in it

    local success, errormessage = pcall(function() -- Redifining again, as last time it was local, so withing that function only
        AbiltiyStore:SetAsync(Playeruserid, data)



    end)
    if success then -- Checking if the saving of data was a success (bool, so true or false)
            print("Data Saved Successfully")
        else -- If check returns false or nil then
            print("The was an error when saving the data")
            warn(errormessage)
        end

end)
game:BindToClose(function()
    local RunService = game:GetService("RunService")
    -- if the current session is studio, do nothing
    if RunService:IsStudio() then
        return
    end

    print("saving player data")

    -- go through all players, saving their data
    local players = game.Players:GetPlayers()
    for _, player in pairs(players) do
        local userId = player.UserId
        local data = AbiltiyStore[player.UserId]
        if data then
            -- wrap in pcall to handle any errors
            local success, result = pcall(function()
                -- SetAsync yields so will stall shutdown
                AbiltiyStore:SetAsync(userId, data)
            end)
            if not success then
                warn(result)
            end    
        end
    end

    print("completed saving player data")

end)

0
Hey it might be because you are testing in studio it dosnt have time to save when you close since you are binding to close sorry for my bad grammer asdfghjk9019 225 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
function DataSavingTest(player)
    local Playeruserid = "Player_"..player.UserId -- Redifining the variable, because the variable before was local
    local data = {player.leaderstats.Ability.Value} -- Creates a table, so we can store numerous stuff in it

    local success, errormessage = pcall(function() -- Redifining again, as last time it was local, so withing that function only
        AbiltiyStore:SetAsync(Playeruserid, data)
    end)
    if success then -- Checking if the saving of data was a success (bool, so true or false)
        print("Data Saved Successfully")
    else -- If check returns false or nil then
        print("The was an error when saving the data")
    end
end

wait(10)
DataSavingTest(game.Players["JeffTheEpicRobloxian"])

add this code

0
just for testing asdfghjk9019 225 — 3y
0
it doesnt work.. do i replace the player removing function with this? JeffTheEpicRobloxian 258 — 3y
Ad

Answer this question