For some reason this datastore is not working for me even when I play the game out of studio. It says the Data Saves but when I rejoin it does not save the value. If anyone has any idea why I would appreciate the help. Thanks.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreSky")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(playerUserId)
end)
if success and data then
Clicks.Value = data[1]
Diamonds.Value = data[2]
else
print("Data not found")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
player.leaderstats.Clicks.Value;
player.leaderstats.Diamonds.Value
}
local success, errormessage = pcall(function()
DataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data saved")
else
print("Error in saving data")
warn(errormessage)
end
end)