DataStore problems

My DataStore is not saving, I believe that is has to do with the the PlayerRemoving since the the output prints the “test 1” but never prints the “test 2.” The game is published and all of the settings are correct. I am not sure how to fix this. Does anyone know?

local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Clicks = Instance.new("StringValue",leader)
	Clicks.Name = "Clicks"

	local data
	local success,errormessage = pcall(function()
		data = ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)
	if success then
		Clicks.Value = data
	else
		print("There was an error while getting data")
		warn(errormessage)
	end
	print(ClicksDataStore:GetAsync(player.UserId.."-Clicks"))
end)
game.Players.PlayerRemoving:Connect(function(player)
	print("test 1")
	local success,errormessage = pcall(function()
		ClicksDataStore:SetAsync(player.UserId.."-Clicks",player.leaderstats.Clicks.Value)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
	print("test 2")
end)

Update: I am able to print the GetAsync (although it is zero) it exists. So the problem must be that the game isn’t actually saving the data. But, its still not working no matter what I do :confused:

2 Likes

Try replacing your current “SetAsync()” function with this:

ClicksDataStore:SetAsync(player.UserId.."-Clicks",player:WaitForChild("leaderstats").Clicks.Value)
1 Like

It doesn’t print it as the server shuts down before you can save it.
You must use BindToClose to save the data.

4 Likes

Simply, you don’t need to name the leaderstats and the clicks, and you may need to replace the instances with this:

local leaderstats = player.leaderstats
local Clicks = leaderstats.Clicks

make sure it looks like this:

	local leaderstats = plr.leaderstats
    local Clicks = leaderstats.Deaths
    local data
    local success, errormessage = pcall(function()
	    data = ClicksDataStore:GetAsync(player.UserId.."-Clicks")
    end)
2 Likes

I tried using this, it prints everything successfully but fails to save the data.

local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Clicks = Instance.new("StringValue",leader)
	Clicks.Name = "Clicks"

	local data
	local success,errormessage = pcall(function()
		data = ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)
	if success then
		Clicks.Value = data
	else
		print("There was an error while getting data")
		warn(errormessage)
	end
	print(ClicksDataStore:GetAsync(player.UserId.."-Clicks"))
end)
game.Players.PlayerRemoving:Connect(function(player)
	print("test 1")
	local success,errormessage = pcall(function()
		ClicksDataStore:SetAsync(player.UserId.."-Clicks",player:WaitForChild("leaderstats").Clicks.Value)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
	print("test 2")
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			ClicksDataStore:SetAsync(player.UserId.."-Clicks",player:WaitForChild("leaderstats").Clicks.Value)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Is the script a global script and inside of the serverscriptservice?

You need the instances since they don’t exist. Can’t use nil.

@zuoec did you enable API services in game settings > security? Many people forget that.

You can use it from a function as long as it’s an existing in-game object. For example:

game.players.playeradded:Connect(function(test)
    game.serverstorage.ScreenGui.parent = test.playerGui
end)
local dataStoreService = game:GetService("DataStoreService")
local ClicksDataStore = dataStoreService:GetDataStore("ClicksDataStore1")

game.Players.PlayerAdded:Connect(function(player)
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
    leader.Parent = player

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
    Clicks.Parent = leader

	local success, result = pcall(function()
		return ClicksDataStore:GetAsync(player.UserId.."-Clicks")
	end)

	if success then
		Clicks.Value = result
	else
		print("There was an error while getting data")
		warn(result)
        Clicks.Value = 0
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success,errormessage = pcall(function()
		ClicksDataStore:UpdateAsync(
           player.UserId.."-Clicks",
           function(old) 
               return player.leaderstats.Clicks.Value 
           end
        )
	    end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
            warn(errormessage)
		end

        if #game.Players:GetPlayers() > 1 then wait(12) end
	end
end)
1 Like

The objects “don’t exist” so it can’t be used. They need to be added first.

The object “test” is not an existing object, but it can be many things, depending on how you trigger the functions.

Try adding a wait() at the end

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			ClicksDataStore:SetAsync(player.UserId.."-Clicks",player:WaitForChild("leaderstats").Clicks.Value)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
	wait(4)
end)
1 Like

Hi!

Everything looks good but you can add the BindToClose function because in the studio if you are a solo developer, the PlayerRemoving event won’t work.

But the thing I was going to say is I assume you are trying to store an integer value.

Here you can just change StringValue to IntValue. Because you can only store strings in StringValue

@zuoec

1 Like

Instead of a stringvalue, i would use a IntValue

1 Like

This didn’t seem to work :confused:
Still no errors but prints Data was saved despite nothing actually saving. :sob:

I’ve done all of this and the product is still fruitless.

Tried it out and worked for me, I can send a place file if it still isn’t working on your side.

1 Like

This is a video of what is happening.

I figured out why your value wasn’t saving. You change the value via the client but not via the server. This means the server is unable to see your value so it can be just seen by only the client.

To run it from the server, you can use RemoteEvent.

3 Likes