Data Store doesnt work when player leaves

So I tried printing at the playerremoving function but nothing works and I dont even know why.
Heres my code

local DataStoreService = game:GetService("DataStoreService")
local StoringDataFile = DataStoreService:GetDataStore("DataFile")

local Players = game:GetService("Players")

local ItemsNumber = 0
local ItemsGlobal = {}

Players.PlayerAdded:Connect(function(LocalPlayer)
    --///MakingTheStoringFolder
    local DataMainFile = Instance.new("Folder",LocalPlayer)
    DataMainFile.Name = "DataMainFile"
    
    if not DataMainFile then
        LocalPlayer:Kick("Something went wrong 😐")
    end
    --//CloningTheItems
    local Totaltems = 0
    local ItemsTable = {}
    
    
    for _,Items in pairs(script:WaitForChild("Items"):GetChildren()) do
        local FoundItem = Items:Clone()
        FoundItem.Parent = DataMainFile
        table.insert(ItemsTable,FoundItem)
        table.insert(ItemsGlobal,FoundItem)
        ItemsNumber += 1
        Totaltems += 1
    end
    
    warn(Totaltems.." Has been found")
    
    
    
    local Information = {}

    
    local Passed,Issue = pcall(function()
        
        local CurrentItemData = 0
        
        for _,GetInformation in pairs(DataMainFile:GetChildren()) do
            CurrentItemData += 1
            local Data
            Data = StoringDataFile:GetAsync(LocalPlayer.UserId.."Information"..CurrentItemData)
            table.insert(Information,Data)
        end
        
        
        
        
    end)
    if Passed then
        local CurrentDataNumber = 0
        
        for DataNum = 1,Totaltems do
            ItemsTable[DataNum].Value = Information[DataNum]
            warn(ItemsTable[DataNum].Value)
        end
        
    elseif not Passed then
        LocalPlayer:Kick("Something went wrong 😐")
        warn(Issue)
    end
end)




Players.PlayerRemoving:Connect(function(RemovedPlayer)
    local Passed,Issue = pcall(function()
        
        for SavingNum = 1,ItemsNumber do
            StoringDataFile:SetAsync(RemovedPlayer.UserId.."Information"..SavingNum,RemovedPlayer.DataMainFile:FindFirstChild(ItemsGlobal[SavingNum]).Value)
            warn("Worked")
        end
    end)
    
    if Passed then
    else
        RemovedPlayer:Kick("Something went wrong 😐")
    end
end)```
1 Like

use the games BindToClose() function to make the entire server wait like 10 seconds before closing. it may be that your game ends all scripts before saving so it doesn’t save.

1 Like

this issue is specifically common when testing in studios. Unless you kick yourself via some way or keeps the game running by being on the perspective of the server instead of the player, the Datastore nearly never saves as the server ends the instance the player leaves.

If you test in a regular server, theres a higher chance it’d work

Hmmm I have never used that could you edit the leaving part to show me a example?

Alright I will test that in a moment

Nevermind I have tried it but still nothing

it wouldn’t be in the leaving part, at the bottom of the script in general you’d wanna write something like

game:BindToclose(function()
    task.wait(5)
end)

Nope I put it at the end of the script but on the removing function only the PCALL function doesnt print

1 Like

Have you toggled Enable Studio Access To API Services in Game Settings?

Wait guys I might have found a way to fix it

Yes I have fixed it thanks for the help guys.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.