I have recently watched a tutorial on how to use the datastore service on roblox, but it doesn't really save correctly. (It prints the error message on that it failed to save when a player leaves.)
Here's 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.valuetwo.Value)
myDataStore:SetAsync(player.UserId.."-three",player.stats.valuethree.Value)
myDataStore:SetAsync(player.UserId.."-four",player.stats.valuefour.Value)
end)
if success then
print("player data saved")
else
print("oh no data save failed")
warn(errormessage)
end
end)
