Yo guys i was making this script and for some reason im getting an error that in pairs(data) is coming as nil can somebody help me?
local DS = game:GetService("DataStoreService")
local MyDataStore = DS:GetDataStore('Infomation')
--this is the folder that contains all the players' value instances
game.Players.PlayerAdded:Connect(function(player)
local dataFolder = player:WaitForChild("CivCars")
--grab data from datastore. (if it doesn't exist then give them the default data folder)
local success,data = pcall(function()
return MyDataStore:GetAsync(player.UserId)
end)
--loop through the players value instances and apply values that you grabbed from datastore.
print(data)
local values = dataFolder:GetChildren()
for i, v in pairs(data) do
local valueInstance = values:WaitForChild(i)
valueInstance.Value = v
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataFolder = player:WaitForChild("CivCars")
local values = dataFolder:GetChildren()
-- a dictionary that will store all the values
local dictionary = {}
--prepare the dictionary
for i = 1, #values do
dictionary[values[i].Name] = values[i].Value
end
print(dictionary)
--save the dictionary
local success = pcall(function()
MyDataStore:SetAsync(player.UserId)
end)
--if player LEFT the game then dispose of their folder
dataFolder:Destroy()
end)