Data store not saving!

Is there anything wrong with my data store to cause this?

game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")
	
	local playerUserID = ID
	local PLRstrength = statistics

	local setSuccess, errorMessage = pcall(function()
		goldStore:SetAsync(playerUserID, PLRstrength)
	end)
	if not setSuccess then
		warn(errorMessage)
	end

	-- Read data store key
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)
1 Like

In this code you are just getting the data store, not saving it.

2 Likes

Isint this the saving of the datastore?

1 Like

The wierd thing is this is what roblox says to do.

Is this a script or a localscript

1 Like

Script in server script service its fired when the player joins

The “save” async is only ran when a player joins, hook it up to a player removing function.

They do.

as you can see this reading the data, not saving.

ooops replied to wrong person srry

as you can see, this says read data, not save data.

local setSuccess, errorMessage = pcall(function()
	goldStore:SetAsync(playerUserID, PLRstrength)
end)

As you can see, this bit of code updates the datastore but it only runs when someone joins, which doesn’t have any save data.

so would I do that when the player leaves?

Heres the problem, something like this wouldent work because it still needs the first few local’s so what do I do?

game.ReplicatedStorage:WaitForChild("PlrLeft").OnServerEvent:Connect(function(player, statistics, ID)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")

	local playerUserID = ID
	local PLRstrength = statistics

	local setSuccess, errorMessage = pcall(function()
		goldStore:SetAsync(playerUserID, PLRstrength)
	end)
	if not setSuccess then
		warn(errorMessage)
	end
end)
game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)

There are 2 ways in you should do:

game.Players.PlayerRemoving:Connect(function(plr)
     playerUserID = plr.UserId
     local setSuccess, errorMessage = pcall(function()
	        goldStore:SetAsync(playerUserID, PLRstrength)
     end)
end)

this one is used to save when the player is leaving

You would be able to do something like that, but you should also make your statistics variable more accessible for your script, that way you would be able to run the standard roblox player functions. (instead of using server/client events)

game:BindToClose(function()
  for i,p in pair game.player:GetPlayers() do
    task.Spawn(function()
       local setSuccess, errorMessage = pcall(function()
	        goldStore:SetAsync(playerUserID, PLRstrength)
       end)
    end
  end
end)

This one is for wehn the server is shut down

Hope this helps!

I followed all the suggestions but it still doesn’t work:

game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")
	local playerUserID = ID
	local PLRstrength = statistics
	game.Players.PlayerRemoving:Connect(function(plr)

		local playerUserID = ID
		local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
		local setSuccess, errorMessage = pcall(function()
			goldStore:SetAsync(playerUserID, PLRstrength)
		end)
		if not setSuccess then
			warn(errorMessage)
		end
	end)

	-- Read data store key
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)

game.ReplicatedStorage:WaitForChild(“PlrJoined”).OnServerEvent:Connect(function(player, statistics, ID)
maybe u can just do players.playeradded instead of firing a remote when a player joins

He needs the statistics & ID variables, I sugested moving them to somewhere accessible so he could do that but yeah…

the problem is that the value deletes itself as soon as I leave so it doesn’t save

game.Players.PlayerAdded:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")
	local playerUserID = player.UserId
	local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
	game.Players.PlayerRemoving:Connect(function(plr)

		local playerUserID = player.UserId
		local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
		local setSuccess, errorMessage = pcall(function()
			goldStore:SetAsync(playerUserID, PLRstrength)
		end)
		if not setSuccess then
			warn(errorMessage)
		end
	end)

	-- Read data store key
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)