Problem With DataStore2

I have a problem with DataStore2.

local DataStore2 = require(1936396537);
local repr = require(3148021300);

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerDataFolder = Instance.new("Folder");
	PlayerDataFolder.Name = Player.Name.."'s Data";
	PlayerDataFolder.Parent = game.ServerScriptService;
	
	local Data = DataStore2("PlayerData", Player);
	
	if Data:Get() ~= nil then
		print(Data:Get());
	end;
	
	game.Players.PlayerRemoving:Connect(function(LeftPlayer)
		if Player.Name == LeftPlayer.Name then
			local PlayerDataFolder = game.ServerScriptService:FindFirstChild(Player.Name.."'s Data");
			
			if PlayerDataFolder then
				local PlayerData = {};
				for _, Tower in ipairs(PlayerDataFolder:GetChildren()) do
					if Tower:IsA("StringValue") then
						print(Tower.Name);
						table.insert(PlayerData, Tower.Name);
					end;
				end;
				Data:Set(PlayerData);
			end;
		end;
	end);
end);

workspace.Part.ClickDetector.MouseClick:Connect(function(Player)
	local PlayerDataFolder = game.ServerScriptService:FindFirstChild(Player.Name.."'s Data");
	
	if PlayerDataFolder then
		local Tower = Instance.new("StringValue");
		Tower.Name = "John";
		Tower.Parent = PlayerDataFolder;
	end;
end);

Everything works fine but at the print(Data:Get()) part it just prints table: blah blah random numbers I want it to get the data that is in the table for example “John”, John", “John” but it doesn’t work tried using repr partially worked but it just printed the entire table not the data.

(It prints the data in studio but not in-game I want it to print the data just like in studio.)

Any help is appreciated.

The reason why print(Data:Get()) is printing table: blah blah random numbers is because the DataStore2:Get() method returns a table object, and printing a table object directly will only print the table’s memory address, not the contents of the table.

To print the contents of the table returned by Data:Get(), you can use a loop to iterate over the table and print each element individually. Here’s an example:

local data = Data:Get() if data ~= nil then for i, v in ipairs(data) do print(i, v) end end

This will print each element of the table on a separate line, along with its index in the table.

Regarding the use of repr, it’s a module that can be used to serialize Lua values to strings, but it’s not necessary in this case since the DataStore2:Get() method already returns a table. If you do want to use repr, you can pass the table returned by Data:Get() to repr.repr() to get a string representation of the table, like this:

local data = Data:Get() if data ~= nil then print(repr.repr(data)) end

However, keep in mind that this will print the entire table as a single string, which may not be what you’re looking for.

Trying this out I will tell you how it goes!

It sadly printed nothing.

I will try using the repr method.

This could be causing the issue. Take the PlayerRemoving event outside of the PlayerAdded event. Make it separate. Even if that does not fix the issue, I would still recommend it.

Then do I get the data with this line local Data = DataStore2("PlayerData", Player);?

I believe so. I’m not completely familiar with DataStore2, so I apologize if I am of little to no help.

Sorry for not responding for a short while my PC is kind of slow… Separating the PlayerRemoving event made my code more cleaner and it didn’t affect the code!

I used the repr method and this is how it turned out.

It printed the entire table but there’s a table called value and it has the data.

I didn’t really expect it to.

At this point, I can no longer help. I apologize with utmost severity, but I am not familiar with DataStore2 at all.

It’s alright I think I will try to get the value table in the table somehow unless someone has a solution.

Seems like another problem has appeared.

When I rejoin the game, the value is the same but when I rejoin again the value is blank.

This is my current code currently if anybody knows how to fix these 2 problems:

local DataStore2 = require(1936396537);
local repr = require(3148021300);

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerDataFolder = Instance.new("Folder");
	PlayerDataFolder.Name = Player.Name.."'s Data";
	PlayerDataFolder.Parent = game.ServerScriptService;
	
	local Data = DataStore2("PlayerData", Player);
	
	if Data:Get() ~= nil then
		print(repr(Data));
		print(repr(Data["value"]));
	end;
end);

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerDataFolder = game.ServerScriptService:FindFirstChild(Player.Name.."'s Data");

	if PlayerDataFolder then
		local PlayerData = {};
		local Data = DataStore2("PlayerData", Player);
		
		for _, Tower in ipairs(PlayerDataFolder:GetChildren()) do
			if Tower:IsA("StringValue") then
				print(Tower.Name);
				table.insert(PlayerData, Tower.Name);
			end;
		end;

		Data:Set(PlayerData);
	end;
end);

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local PlayerDataFolder = game.ServerScriptService:FindFirstChild(Player.Name.."'s Data");

		if PlayerDataFolder then
			local PlayerData = {};
			local Data = DataStore2("PlayerData", Player);
			
			for _, Tower in ipairs(PlayerDataFolder:GetChildren()) do
				if Tower:IsA("StringValue") then
					print(Tower.Name);
					table.insert(PlayerData, Tower.Name);
				end;
			end;

			Data:Set(PlayerData);
		end;
	end;
end);

workspace.Part.ClickDetector.MouseClick:Connect(function(Player)
	local PlayerDataFolder = game.ServerScriptService:FindFirstChild(Player.Name.."'s Data");
	
	if PlayerDataFolder then
		local Tower = Instance.new("StringValue");
		Tower.Name = "John";
		Tower.Parent = PlayerDataFolder;
	end;
end);

I remember somebody helping me with a DataStore that can save strings using DataStore2 and I just happened to find it.

I will rewrite some of the code, etc to make it better.

Shoutout to @GetGlobals for helping me before!

:heart:

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