Data Store problem

Script:

local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("mydatastore")


game.Players.PlayerAdded:Connect(function(player)
	
	local playerid = "Player_" .. player.UserId
	local data = mydatastore:GetAsync(playerid)
	
	local ld = Instance.new("Folder", player)
	ld.Name = "leaderstats"

	local rebirthCount = Instance.new("IntValue", ld)
	rebirthCount.Name = "Rebirth"
	
	local damageCount = Instance.new("IntValue", ld)
	damageCount.Name = "Damage"

	local plusDamage = Instance.new("IntValue", ld)
	plusDamage.Name = "PlusDamage"

	local wins = Instance.new("IntValue", ld)
	wins.Name = "Wins"
	
	if data then
		rebirthCount.Value = data["Rebirth"]
		damageCount.Value = data["Damage"]
		plusDamage.Value = data["PlusDamage"]
		wins.Value = data["Wins"]
	else
		rebirthCount.Value = 0
		damageCount.Value = 0
		plusDamage.Value = 1
		wins.Value = 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local datatobesaved = {
		rebirth = player.leaderstats.Rebirth.Value,
		damage = player.leaderstats.Damage.Value,
		plusDamage = player.leaderstats.PlusDamage.Value,
		win = player.leaderstats.Wins.Value
	}
	
	local playerid = "Player_" .. player.UserId
	
	print("SS", player, playerid)
	
	local success, err = pcall(function()
		mydatastore:SetAsync(playerid, datatobesaved)
	end)
	
	print(success)
	
	if success then
		print("data saved!")
	else
		print("data faield to save!")
	end
end)

Good afternoon, I found a guide on devForum where I found information on how to make good data saving. But for some reason they are not saved, here is the guide if anything: How to To save Data - Tutorial

If anything, in the settings I enabled everything as it should be, and the problem is that the first print is output and the second is not, here is the Output.

I mean, it doesn’t want to work, the question is why?

2 Likes

When you are accessing data you uppercase the first letter.

But when you save the data you use a lowercase letter, which I think is the problem. Instead upper case the data so it looks like this:

local datatobesaved = {
		Rebirth = player.leaderstats.Rebirth.Value,
		Damage = player.leaderstats.Damage.Value,
		PlusDamage = player.leaderstats.PlusDamage.Value,
		Win = player.leaderstats.Wins.Value
	}
1 Like

no, that didn’t help, moreover, I decided to check if I was logged into the function and this is what came out:

local success, err = pcall(function()
		print("saving function")
		mydatastore:SetAsync(playerid, datatobesaved)
		print(mydatastore, "saving")
	end)
	

1 Like

Remove the pcall and check what the output says, that way you can see the error message if there is one

1 Like

How exactly am I supposed to do that, can you give me an example?

1 Like

Remove this

And this

Then you should see the error

Also comment out the lines that use the variables while debugging

1 Like

if you mean for me to do this:

local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("mydatastore")


game.Players.PlayerAdded:Connect(function(player)
	
	local playerid = "Player_" .. player.UserId
	local data = mydatastore:GetAsync(playerid)
	
	local ld = Instance.new("Folder", player)
	ld.Name = "leaderstats"

	local rebirthCount = Instance.new("IntValue", ld)
	rebirthCount.Name = "Rebirth"
	
	local damageCount = Instance.new("IntValue", ld)
	damageCount.Name = "Damage"

	local plusDamage = Instance.new("IntValue", ld)
	plusDamage.Name = "PlusDamage"

	local wins = Instance.new("IntValue", ld)
	wins.Name = "Wins"
	
	if data then
		rebirthCount.Value = data["Rebirth"]
		damageCount.Value = data["Damage"]
		plusDamage.Value = data["PlusDamage"]
		wins.Value = data["Wins"]
	else
		rebirthCount.Value = 0
		damageCount.Value = 0
		plusDamage.Value = 1
		wins.Value = 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local datatobesaved = {
		Rebirth = player.leaderstats.Rebirth.Value,
		Damage = player.leaderstats.Damage.Value,
		PlusDamage = player.leaderstats.PlusDamage.Value,
		Win = player.leaderstats.Wins.Value
	}
	
	local playerid = "Player_" .. player.UserId
	
	print("SS", player, playerid)
	
	--local success, err = pcall(function()
	--	print("saving function")
	--	mydatastore:SetAsync(playerid, datatobesaved)
	--	print(mydatastore, "saving")
	--end)
	
	--print(success)
	
	--if success then
	--	print("data saved!")
	--else
	--	print("data faield to save!", err)
	--end
end)

then what is the point if everything works without it, but the data are not saved, because I save them just in β€œsuccess”.

1 Like

keep these lines uncommented, then try

You mean like this:

game.Players.PlayerRemoving:Connect(function(player)
	
	local datatobesaved = {
		Rebirth = player.leaderstats.Rebirth.Value,
		Damage = player.leaderstats.Damage.Value,
		PlusDamage = player.leaderstats.PlusDamage.Value,
		Win = player.leaderstats.Wins.Value
	}
	
	local playerid = "Player_" .. player.UserId
	
	print("SS", player, playerid)
	
	local success, err = pcall(function()
		print("saving function")
		--mydatastore:SetAsync(playerid, datatobesaved)
		--print(mydatastore, "saving")
	end)
	
	print(success)
	
	if success then
		print("data saved!")
	else
		print("data failed to save!", err)
	end
end)

If yes then I have this:

local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("mydatastore")


game.Players.PlayerAdded:Connect(function(player)
	
	local playerid = "Player_" .. player.UserId
	local data = mydatastore:GetAsync(playerid)
	
	local ld = Instance.new("Folder", player)
	ld.Name = "leaderstats"

	local rebirthCount = Instance.new("IntValue", ld)
	rebirthCount.Name = "Rebirth"
	
	local damageCount = Instance.new("IntValue", ld)
	damageCount.Name = "Damage"

	local plusDamage = Instance.new("IntValue", ld)
	plusDamage.Name = "PlusDamage"

	local wins = Instance.new("IntValue", ld)
	wins.Name = "Wins"
	
	if data then
		rebirthCount.Value = data["Rebirth"]
		damageCount.Value = data["Damage"]
		plusDamage.Value = data["PlusDamage"]
		wins.Value = data["Wins"]
	else
		rebirthCount.Value = 0
		damageCount.Value = 0
		plusDamage.Value = 1
		wins.Value = 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local datatobesaved = {
		rebirth = player.leaderstats.Rebirth.Value,
		damage = player.leaderstats.Damage.Value,
		plusDamage = player.leaderstats.PlusDamage.Value,
		win = player.leaderstats.Wins.Value
	}
	
	local playerid = "Player_" .. player.UserId
	
	print("SS", player, playerid)
		mydatastore:SetAsync(playerid, datatobesaved)

print("saved")

end)

here

No, it doesn’t work. Here’s what it says:

Instead of what you have now which is this:
if data then
rebirthCount.Value = data[β€œRebirth”]
damageCount.Value = data[β€œDamage”]
plusDamage.Value = data[β€œPlusDamage”]
wins.Value = data[β€œWins”]
else
rebirthCount.Value = 0
damageCount.Value = 0
plusDamage.Value = 1
wins.Value = 0
end

Try this:
rebirthCount.Value = data[Rebirth]| or 0
damageCount.Value = data[Damage]| or 1
plusDamage.Value = data[PlusDamage]| or 0
wins.Value = data[Wins]| or 0

Thats really weird, its stopping at that part. It should error if something goes wrong

I refreshed your script

local datastoreservice = game:GetService("DataStoreService")
local database = datastoreservice:GetDataStore("data")
local SessionData = {}
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local rebirthCount = Instance.new("IntValue", leaderstats)
	rebirthCount.Name = "Rebirth"
	
local damageCount = Instance.new("IntValue", leaderstats)
	damageCount.Name = "Damage"

local wins= Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"

local plusDamage = Instance.new("IntValue", leaderstats)
	plusDamage.Name = "PlusDamage"

	local succses = nil
	local 	PlayerData = nil
	local attempt = 1

	repeat
		succses,PlayerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		attempt += 1
		if not succses then
			warn(PlayerData)
			task.wait(3)
		end
	until succses or attempt == 5

	if succses then
		print("Connected Data")
		if not PlayerData then
			print("Default Data Loaded")
			PlayerData = {
				["Rebirth"] = 0,
				["Damage"] = 0,
["PlusDamage"] = 1,
["Wins"] = 0
			}
		end
		SessionData[player.UserId] = PlayerData
	else
		warn("Unable Get Data For", player.UserId)
		player:Kick("Unable To Load Your Data, Try Rejoining")
	end
	rebirthCount.Value = SessionData[player.UserId].Rebirth
	rebirthCount.Changed:Connect(function()
		SessionData[player.UserId].Rebirth = rebirthCount.Value
	end)
	damageCount.Value = SessionData[player.UserId].Damage
	damageCount.Changed:Connect(function()
		SessionData[player.UserId].Damage = damageCount.Value
	end)
	plusDamage.Value = SessionData[player.UserId].PlusDamage
	plusDamage.Changed:Connect(function()
		SessionData[player.UserId].PlusDamage= plusDamage.Value
	end)
	wins.Value = SessionData[player.UserId].Wins
	damageCount.Changed:Connect(function()
		SessionData[player.UserId].Wins= wins.Value
	end)
end)

function playerLeaving(player)
	if SessionData[player.UserId] then
		local succses = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			succses,errorMsg = pcall(function()
				database:SetAsync(player.UserId, SessionData[player.UserId])
			end)
			attempt += 1
			if not succses then
				warn(errorMsg)
				task.wait(3)
			end
		until succses or attempt == 5

		if succses then
			print("Data Saved for ", player.Name)
		else
			warn("Unable to save for", player.Name)
		end
	end
end

game.Players.PlayerRemoving:Connect(playerLeaving)

function serverShutdown()
	print("Server Shutting Down")
	for i,player in ipairs(game.Players:GetPlayers()) do
		task.spawn(function()
			playerLeaving(player)
		end)

	end
end

game:BindToClose(serverShutdown)

There can be grammar errors i didnt edited in studio

1 Like