Need help with Datastore

Hello, Scripting Support. I once again request your help. I am trying to figure out the problem with my Datastore. I have not found a solution on the forums, so I decided to ask directly.

local sessiondata = {

}

local function savedata(player)
	if sessiondata[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempts = 1
		repeat
			success,errorMsg = pcall(function()
				datastore:SetAsync(player.UserId.."-Data", sessiondata[player.UserId])
			end)
			attempts += 1

			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempts == 5

		if success then
			print(player.Name .. ", Data Saved")
		else
			warn(player.Name .. ", Issue Saving Data")
		end
	end
end

local function loaddata(player)
	local attempts = 0
	local success = nil
	local playerdata = nil
	repeat
		success, playerdata = pcall(function()
			return datastore:GetAsync(player.UserId.."-Data")
		end)

		attempts += 1
		if not success then
			warn(playerdata)
			task.wait(3)
		end
	until attempts == 5 or success	
	if success then
		print(player.Name .. ", data successfully loaded!")
		if not playerdata then
			playerdata = {
				["Time"] = 0
			}
		end
		sessiondata[player.UserId] = playerdata
	else
		player:Kick("Data could not be loaded. Try again later!")
	end
end

players.PlayerAdded:Connect(function(player)
	loaddata(player)

	local data = sessiondata[player.UserId]

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

	local playertime = Instance.new("IntValue")
	playertime.Name = "Time"
	playertime.Parent = leaderstats
	playertime.Value = data["Time"]
	
	playertime.Changed:Connect(function()
		data["Time"] = playertime.Value
	end)
	
	while task.wait(1) do
		playertime.Value += 1
	end
end)

game:BindToClose(function()
	for i,player in pairs(players:GetPlayers()) do
		task.spawn(function()
			savedata(player)
		end)
	end
end)

players.PlayerRemoving:Connect(function(player)		
	savedata(player)
end)

This is all the related code. But the main problem is Saving. The game only seems to save data around half the time. I can’t for the life of me figure out what the problem might be. Help would be appreciated. Many thanks.

3 Likes

I really need help with this. It’s a big problem.

2 Likes

maybe try use

coroutine.wrap(function()
	while task.wait(1) do
		playertime.Value += 1
	end
end)()

since the playerremoving function probably doesnt function since it always looping

2 Likes

When it doesn’t save your data does it do the warn, or does it do nothing?

2 Likes

It does nothing. Nothing is printed in the output.

1 Like

I will try this soon. Thanks for the idea!

1 Like

Tried this, and it sadly didn’t work.

2 Likes

If nothing is printed in the output that means there probably isn’t a sessiondata for that player. I think this is because when u press Play in studio your player will also be the server so it doesn’t fire the PlayerAdded because your player joined before that event was run. Try using Team Test because that will open a separate server before your player joins.

1 Like

Could you try adding some more wait to the BindToClose function?
If it really saves 50% of the time, it might be related to that.

game:BindToClose(function()
	for i,player in pairs(players:GetPlayers()) do
		task.spawn(function()
			savedata(player)
		end)
	end
    if RunService:IsStudio() then 
		wait(3)
    end
end)
1 Like

There’s a print if session data isn’t found for the player now, and then it still wont print anything. So this can’t be the problem.

Just tried this, and it didn’t work.

u dont want to use profileservice ? it should fix your problem i guess ?

Profile-Service-Guide.rbxl (67.4 KB)

I would prefer not to use a plugin for datastores.

I’m not sure if you still need help with this since i’m a little late, but if you do, are there any warnings notifying you of ratelimits in your Developer Console (F9)?

No, there aren’t any sort of warnings.

Are you trying to save/load data from a Play Test in Studio?

Yes, that is what I am trying to do.

I’ve had issues with data saving/loading inconsistently in Studio. I believe this is caused by the server shutting down too quickly. If you publish the game and try to save data through the game client, it should save and load properly.

Try this:

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("PlayerDataStore")

local sessiondata = {}

local function savedata(player)
    if sessiondata[player.UserId] then
        local success = nil
        local errorMsg = nil
        local attempts = 1
        repeat
            success, errorMsg = pcall(function()
                datastore:SetAsync(player.UserId.."-Data", sessiondata[player.UserId])
            end)
            attempts += 1

            if not success then
                warn(errorMsg)
                wait(3)
            end
        until success or attempts == 5

        if success then
            print(player.Name .. ", Data Saved")
        else
            warn(player.Name .. ", Issue Saving Data")
        end
    end
end

local function loaddata(player)
    local attempts = 0
    local success = nil
    local playerdata = nil
    repeat
        success, playerdata = pcall(function()
            return datastore:GetAsync(player.UserId.."-Data")
        end)

        attempts += 1
        if not success then
            warn(playerdata)
            wait(3)
        end
    until attempts == 5 or success

    if success then
        print(player.Name .. ", data successfully loaded!")
        if not playerdata then
            playerdata = {
                ["Time"] = 0
            }
        end
        sessiondata[player.UserId] = playerdata
    else
        player:Kick("Data could not be loaded. Try again later!")
    end
end

game.Players.PlayerAdded:Connect(function(player)
    loaddata(player)

    local data = sessiondata[player.UserId]

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

    local playertime = Instance.new("IntValue")
    playertime.Name = "Time"
    playertime.Parent = leaderstats
    playertime.Value = data["Time"]

    playertime.Changed:Connect(function()
        data["Time"] = playertime.Value
    end)

	spawn(function()
    	while wait(1) do
    	    playertime.Value += 1
    	end
	end)
end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        task.spawn(function()
            savedata(player)
        end)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    savedata(player)
end)
1 Like