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.)
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.
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.
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.
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);