Value breaks on data storing

I went ahead and made data stores for my game and every time I set my value to anything through the server or client, the coconut and XP values don’t save, and the catfood value automatically becomes -9223372036854775808 and I have no idea why this happens. If I’m doing something wrong, please let me know. Here’s the script (server script in serverscriptservice):

local DataStoreService = game:GetService("DataStoreService")
local CatFoodDataStore = DataStoreService:GetDataStore("CatFoodDataStore")
local XPDataStore = DataStoreService:GetDataStore("XPDataStore")
local CoconutsDataStore = DataStoreService:GetDataStore("CoconutsDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Archivable = true
	leaderstats.Parent = plr
	local CatfoodValue = Instance.new("IntValue")
	CatfoodValue.Name = "CatFood"
	CatfoodValue.Archivable = true
	CatfoodValue.Parent = leaderstats
	local XPValue = Instance.new("IntValue")
	XPValue.Name = "XP"
	XPValue.Archivable = true
	XPValue.Parent = leaderstats
	local CoconutsValue = Instance.new("IntValue")
	CoconutsValue.Name = "Coconuts"
	CoconutsValue.Archivable = true
	CoconutsValue.Parent = leaderstats
	local CatFoodData
	local XPData
	local CoconutsData
	local success, errormessage = pcall(function()
		CatFoodData = CatFoodDataStore:GetAsync(plr.UserId.."-CatFood")
		XPData = XPDataStore:GetAsync(plr.UserId.."-XP")
		CoconutsData = CoconutsDataStore:GetAsync(plr.UserId.."-Coconuts")
	end)
	if success then
		CatfoodValue.Value = CatFoodData
		XPValue.Value = XPData
		CoconutsValue.Value = CoconutsData
	else
		print("There was an error getting data!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		CatFoodDataStore:SetAsync(plr.UserId.."-CatFood",plr.leaderstats.CatFood.Value)
		XPDataStore:SetAsync(plr.UserId.."-XP",plr.leaderstats.XP.Value)
		CoconutsDataStore:SetAsync(plr.UserId.."-Coconuts",plr.leaderstats.Coconuts.Value)
	end)

	if success then
		print("Data succesfully saved!")
	else
		print("There was an error saving data!")
		warn(errormessage)
	end
end)

proof picture:


proof clip:
https://streamable.com/aqvhgu

This looks like it should work. Try resetting the dataStore for your ID to 0 on all of the values than test it again.

It doesn’t work :confused:, it keeps breaking.

It looks like its not even saving the data. Try with game:BindToClose

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Data1 = DataStoreService:GetDataStore("Data1")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Leaderstats = Instance.new("Folder",Player)
		Leaderstats.Name = "leaderstats"
		
		local Points = Instance.new("IntValue",Leaderstats)
		Points.Name = "Points"
		
		local Data 
		local PlayerID = "Player_"..Player.UserId
		local Success,ErrorMessage = pcall(function()
			Data = Data1:GetAsync(PlayerID)
		end)
		
		if Success then
			if Data then
				Points.Value = Data.Points
				wait(1)
				Character:WaitForChild("HumanoidRootPart").Position = Vector3.new(Data.Position:match("(.+), (.+), (.+)"))
			end
		else
			warn(ErrorMessage)
		end
	end)
end)

function SaveData(player)
	local Data = {
		Points = player.leaderstats.Points.Value,
		Position = tostring(player.Character.HumanoidRootPart.Position)
	}
	local PlayerID = "Player_"..player.UserId
	local Success,ErrorMessage = pcall(function()
		Data1:SetAsync(PlayerID,Data)
	end)
end

Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		SaveData(player)
	end
end)

This is how i save data!

2 Likes

If I want to save multiple data do I just add more lines in the data table?

Yes , and dont forget to load it too!

Alright, I’m gonna try this. Thanks! :smiley:

It did not work :frowning:

I might have done something wrong in the script, here’s the script:

local DataStoreService = game:GetService("DataStoreService")
local CatFoodDataStore = DataStoreService:GetDataStore("CatFoodDataStore")
local XPDataStore = DataStoreService:GetDataStore("XPDataStore")
local CoconutsDataStore = DataStoreService:GetDataStore("CoconutsDataStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Archivable = true
		leaderstats.Parent = plr
		local CatfoodValue = Instance.new("IntValue")
		CatfoodValue.Name = "CatFood"
		CatfoodValue.Archivable = true
		CatfoodValue.Parent = leaderstats
		local XPValue = Instance.new("IntValue")
		XPValue.Name = "XP"
		XPValue.Archivable = true
		XPValue.Parent = leaderstats
		local CoconutsValue = Instance.new("IntValue")
		CoconutsValue.Name = "Coconuts"
		CoconutsValue.Archivable = true
		CoconutsValue.Parent = leaderstats
		local CatFoodData
		local XPData
		local CoconutsData
		local PlayerID = "Player_"..plr.UserId
		local success, errormessage = pcall(function()
			CatFoodData = CatFoodDataStore:GetAsync(PlayerID)
			XPData = XPDataStore:GetAsync(PlayerID)
			CoconutsData = CoconutsDataStore:GetAsync(PlayerID)
		end)
		if success then
			if CatFoodData and XPData and CoconutsData then
				CatfoodValue.Value = CatFoodData.CatFood
				XPValue.Value = XPData.XP
				CoconutsValue.Value = CoconutsData.Coconuts
			end
		else
			warn(errormessage)
		end
	end)
end)

function SaveData(plr)
	local CatFoodData = {
		CatFood = plr.leaderstats.CatFood.Value,
	}
	local XPData = {
		XP = plr.leaderstats.XP.Value,
	}
	local CoconutsData = {
		Coconuts = plr.leaderstats.Coconuts.Value,
	}
	local PlayerID = "Player_"..plr.UserId
	local Success,ErrorMessage = pcall(function()
		CatFoodDataStore:SetAsync(PlayerID,CatFoodData)
		XPDataStore:SetAsync(PlayerID,XPData)
		CoconutsDataStore:SetAsync(PlayerID,CoconutsData)
	end)
end

Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
	for _, plr in ipairs(Players:GetPlayers()) do
		SaveData(plr)
	end
end)

The CatFood value dropped to 0 as well now!

Hmm… Weird i tried the exact same script and it works for me it saved all 3 values and loaded them.

That’s so weird, is there something I’m doing wrong? My script is in serverscriptservice is it supposed to be there?

Yes i had it in there(random words for char limit)

This must be a bug then. What can I do now? D:

Idk, it could be a bug or maybe it could a network issue who knows.

I got a message! This is the message:
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Player_637561750 - Studio

My value saved! Finally! Thank you for helping :smiley:

Its not really a error its a warning its from the Game:BindToClose() if u want to get rid of it i think u need to get RunService and do :IsStudio() or something but i dont think it really matters i also get that but it saves and loads the data!

1 Like