DataStore stopped saving and loading?

Hello all!

The title is self-explanatory. Why does my data not save or load?

Here is my ServerScript, inside of ServerScriptService:

local dataStoreService = game:GetService("DataStoreService")

local datastore = dataStoreService:GetDataStore("File1")
local pointStat

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	pointStat = leaderstats:WaitForChild("Points")
	
	if player then

		print("yippe")
		local data

		local success, response = pcall(function()
			data = datastore:GetAsync("file1")
		end)

		if success then
			pointStat.Value = data
		else
			pointStat.Value = 0	
			warn("Error.")
		end

	end
end)

game.Players.PlayerRemoving:Connect(function()
	local success, response = pcall(function()
		datastore:SetAsync("file1", pointStat.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end)

Any help is appreciated, thank you.

2 Likes

try adding game:BindToClose()

game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
	local success, response = pcall(function()
		datastore:SetAsync(player.UserId, pointStat.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end
end)

also please use the player.UserId for your data keys

This method sadly did not work

replace “file1” with player.UserId also make sure api services are enabled

Your not getting the player when removing maybe

pretty sure playerremoving doesnt fire when theres 1 person in a server thats why i mentioned game:BindToClose()

PlayerRemoving was working before, but I feel like i mightve corrupted my data store or something

also api Services is on, and I tried the UserId name but it didnt work :expressionless:

r u getting any errors in the output?

There are no errors in the output

local dataStoreService = game:GetService("DataStoreService")

local datastore = dataStoreService:GetDataStore("File1")
local pointStat

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	pointStat = leaderstats:WaitForChild("Points")
	
	if player then

		print("yippe")
		local data

		local success, response = pcall(function()
			data = datastore:GetAsync(player.UserId)
		end)

		if success and data then
			pointStat.Value = data[1]
		else
			pointStat.Value = 0	
			warn("Error.")
		end

	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, response = pcall(function()
		datastore:SetAsync(player.UserId, pointStat.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end)

game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
	local success, response = pcall(function()
		datastore:SetAsync(player.UserId, pointStat.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end
end)

also one question where r u getting the leaderstats and pointstat from?

1 Like

I made the script for adding leaderstats in another ServerScript

local dataStoreService = game:GetService("DataStoreService")

local datastore = dataStoreService:GetDataStore("File1")
local pointStat

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = player:WaitForChild("leaderstats")
	pointStat = leaderstats:WaitForChild("Points")
	
	if player then

		print("yippe")
		local data

		local success, response = pcall(function()
			data = datastore:GetAsync(plr.UserId)
		end)

		if success then
			pointStat.Value = data
		else
			pointStat.Value = 0	
			warn("Error.")
		end

	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, response = pcall(function()
		datastore:SetAsync(plr.UserId, plr.leaderstats.pointStat.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end)

try this code and lemme know if it works

In my script in gets the player userid when joinging, and when removing its get the player user id too and updates the leaderstats

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