I have a script that loads and saves a few boolvalues, but some of these values cannot be loaded properly for some reason. The pcall function always ends up failing and printing the error message in the output.
Here is my code:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "stats"
stats.Parent = player
local valuetwo = Instance.new("BoolValue")
valuetwo.Name = "two"
valuetwo.Parent = stats
local valuethree = Instance.new("BoolValue")
valuethree.Name = "three"
valuethree.Parent = stats
local valuefour = Instance.new("BoolValue")
valuefour.Name = "four"
valuefour.Parent = stats
local data2
local data3
local data4
local success, errormessage = pcall(function()
data2 = myDataStore:GetAsync(player.UserId.."-two")
data3 = myDataStore:GetAsync(player.UserId.."-three")
data4 = myDataStore:GetAsync(player.UserId.."-four")
end)
if success then
valuetwo.Value = data2
valuethree.Value = data3
valuefour.Value = data4
else
print("data failed to load")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-two" , player.stats.two.Value)
myDataStore:SetAsync(player.UserId.."-three" , player.stats.three.Value)
myDataStore:SetAsync(player.UserId.."-four" , player.stats.four.Value)
end)
if success then
print("player data saved")
else
print("oh no data save failed")
warn(errormessage)
end
end)
Output: "data failed to load"

2 comments
Make sure you have Studio access to API services enabled.
Still doesn't work. The error message is no longer printed, but it doesn't actually save.