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

Datastore script wont save data?

Asked by 1 year ago

I've been working on this for like 5 days and can't figure it out because I have tiny brain. I've asked for help here multiple times but no matter what I do it won't work. I know the saving won't work because the data loading works completely fine.

Here's the code I have:

-- Written by MattVSNNL 

local PlayersService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local boolDataStore = dataStoreService:GetDataStore("BoolDataStore")

PlayersService.PlayerAdded:Connect(function(player) 

    local onMenu = Instance.new("BoolValue") 
    onMenu.Name = "onMenu" 
    onMenu.Parent = player 

    local onMenuData = nil 

    pcall(function() 
        onMenuData = boolDataStore:GetAsync(player.UserId.."-onMenu") 
    end)

    if onMenuData ~= nil then 

        onMenu.Value = onMenuData 
    else
        onMenu.Value = true 
    end

end)

local bindableEvent = Instance.new("BindableEvent") 

PlayersService.PlayerRemoving:Connect(function(player) 

    pcall(function() 
        boolDataStore:SetAsync(player.UserId.."-onMenu", player.onMenu.Value) 
    end)

    bindableEvent:Fire() 
    print("Saved!") 

end)

game:BindToClose(function() 
    while #PlayersService:GetPlayers() > 0 do 
        bindableEvent.Event:Wait() 
        wait() 
    end
end)
0
Could you try printing the success and error message returned from the pcall? (local success, errorMessage = pcall(...) 7z99 203 — 1y
0
^Also you aren't ever calling the save function inside of your game:BindToClose function. 7z99 203 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

Hello! Here is what you need to do:

-- Written by MattVSNNL

local PlayersService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local boolDataStore = dataStoreService:GetDataStore("BoolDataStore")

PlayersService.PlayerAdded:Connect(function(player) 

    local onMenu = Instance.new("BoolValue") 
    onMenu.Name = "onMenu" 
    onMenu.Parent = player 

   onMenu.Changed:Connect(function()
        boolDataStore:SetAsync(plr.UserId, onMenu.Value)
    end)

end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.onMenu.Value) 
end)

Also make sure that "Enable Studio Access to API Services" is enabled in your game.

I'm not 100% sure if it's going to work, but if it does not, I'm here to help!

-KoalaKo_XD

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

Make sure "Enable Studio Access to API Services" is enabled in your game settings.

-- Written by MattVSNNL 

local PlayersService = game:GetService("Players")
local RunService = game:GetService("RunService")
local dataStoreService = game:GetService("DataStoreService")

local boolDataStore = dataStoreService:GetDataStore("BoolDataStore")

function loadData(player)
    local key = player.UserId .. "-onMenu"

    local onMenu = Instance.new("BoolValue")
    onMenu.Name = "onMenu"
    onMenu.Parent = player

    local success, data

    repeat
        success, data = pcall(boolDataStore.GetAsync, boolDataStore, key)
    until success or not PlayersService:FindFirstChild(player.Name)

    if success then
        onMenu.Value = data or true
    end
end

function saveData(player)
    local key = player.UserId .. "-onMenu"
    local onMenu = player:FindFirstChild("onMenu")

    if onMenu then
        local onMenuValue = onMenu.Value
        local success, result

        repeat
            success, result = pcall(boolDataStore.UpdateAsync, boolDataStore, key, function() return onMenuValue end)
        until success
    end
end

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

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

        finished.Event:Wait()
    end
end)
0
Ive added prints but it still wont save even though the prints are saying its saving correctly. YourAverageMyth 45 — 1y
0
try using updateasync, i edited the script to updateasync T3_MasterGamer 2189 — 1y

Answer this question