Hello Roblox Dev Community,
Info
So i have been testing out tables inside of datastores,
there seems to be no problems in the output but the datastore just doesn’t gets the data.
It gives me back ‘nil’.
Whole Script
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("TableTesting")
local plrs = game:GetService("Players")
local foodTable = {"apple", "banana", "mango"}
local function saveData(plr)
local saveTable = {}
for _, food in pairs(plr.FoodFolder:GetChildren()) do
if food:IsA("StringValue") then
if not table.find(saveTable, food.Name) then
table.insert(saveTable, food.Name)
end
end
end
if saveTable == nil then
saveTable = foodTable
print("No info found inside of "..plr.Name)
end
for i, v in pairs(saveTable) do
print("index: ", i)
print("value: ", v)
end
local success, notSuccess = pcall(function()
ds:SetAsync(saveTable, plr.UserId.."-FoodTable")
end)
if success then
print(plr.Name.."'s foodTable saved well!")
else
print(plr.Name.."'s foodTable didn't save!")
warn(notSuccess)
end
end
game.Players.PlayerAdded: Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "FoodFolder"
folder.Parent = plr
local getData
local success, notSuccess = pcall(function()
getData = ds:GetAsync(plr.UserId.."-FoodTable")
end)
if getData == nil then
print(plr.Name.." doesn't got any food.")
getData = foodTable
end
if success then
print("Successfully imported plr "..plr.Name.."'s data!")
for i, v in pairs(getData) do
print("index: "..i)
print("value: "..v)
local newFood = Instance.new("StringValue")
newFood.Name = v
newFood.Parent = plr.FoodFolder
end
else
print(plr.Name.."'s data didn't get imported successfully!")
warn(notSuccess)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, notSuccess = pcall(function()
saveData(plr)
end)
if not success then
warn(notSuccess)
end
end)
game:BindToClose(function()
wait(2)
end)
DISCLAIMER
For tests i added after i joined a StringValue on the serverSide to the foodFolder. ‘kiwi’
FoodFolder how it looks
Without the kiwi
With the kiwi
The Output after leaving
The nil problem repeats everytime i do this, and it never gives me a error or print of mine that says that something gone wrong.
The script has been made as a server-sided script, and everytime i added a stringValue to that folder i did it through this command server-sided
Command
Instance.new("StringValue", game.Players.NotSpideeyDev.FoodFolder).Name = "Kiwi"
I have tried to test this in the real game in roblox and in roblox studio itself.
Achieve
So what i want to achieve here is that when i rejoin it doesn’t gives me ‘nil’ and that the table add’s that kiwi one to the output. So like, index: 4 value: kiwi, Just like in the output when i leave.
Hope everyone has an good day, stay save and healthy!