for some reason this script does not work even though im confident everything is right,
the print errormessage is nil
local datastoreservice = game:GetService("DataStoreService")
local StreamStatsDataStore = datastoreservice:GetDataStore("StreamStats")
local players = game.Players
players.PlayerAdded:Connect(function(plr)
local StreamStats = Instance.new("Folder")
StreamStats.Name = "StreamStats"
StreamStats.Parent = plr
local Followers = Instance.new("IntValue")
Followers.Parent = StreamStats
Followers.Name = "Followers"
local Subscribers = Instance.new("IntValue")
Subscribers.Parent = StreamStats
Subscribers.Name = "Subscribers"
local data
local successfull, errormessage = pcall(function()
data = StreamStatsDataStore:GetAsync(plr.UserId)
end)
if successfull and data then
Followers.Value = data.Followers
Subscribers.Value = data.Subscribers
print("Data Loaded Successfully")
else
Subscribers.Value = 0
Followers.Value = 0
warn(errormessage)
end
end)
players.PlayerRemoving:Connect(function(plr)
local successfull, errormessage = pcall(function()
local data = {
Subscribers = plr.StreamStats.Subscribers.Value;
Followers = plr.StreamStats.Followers.Value;
}
StreamStatsDataStore:SetAsync(plr.UserId, data)
end)
if successfull then
print("Data Saved Successfully")
else
print("Encountered An Error While Saving Data")
end
end)
i dont want it to be in leaderstats because i dont want these values to be visible. also i have other values in leaderstats that i want to be visible so i cant hide the leaderstats folder
local dataStoreService = game:GetService("DataStoreService")
local FollowersDataStore = dataStoreService:GetDataStore("Followers")
local SubscribersDataStore = dataStoreService:GetDataStore("Subscribers")
game.Players.PlayerAdded:Connect(function(player)
local StreamStats = Instance.new("Folder", player) -- Creating StreamStats folder inside player
StreamStats.Name = "StreamStats"
local Followers = Instance.new("IntValue", StreamStats) -- Creating intValue inside leaderstats with name Followers
Followers.Name = "Followers"
Followers.Value = 0
local Subscribers = Instance.new("IntValue", StreamStats) -- Creating intValue inside leaderstats with name Subscribers
Subscribers.Name = "Subscribers"
Subscribers.Value = 0
local playerUserId = "player_"..player.UserId
-- Loading Followers Data
local FollowersData
local success, errormessage = pcall(function()
FollowersData = FollowersDataStore:GetAsync(playerUserId)
end)
if success then
Followers.Value = FollowersData
end
-- Loading Subscribers Data
local SubscribersData
local success, errormessage = pcall(function()
SubscribersData = SubscribersDataStore:GetAsync(playerUserId)
end)
if success then
Subscribers.Value = SubscribersData
end
end)
-- Saving Data
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "player_"..player.UserId
-- Saving Followers
local FollowersValue = player.leaderstats.Followers.Value
local success, errormessage = pcall(function()
FollowersDataStore:SetAsync(playerUserId, FollowersValue)
end)
-- Saving Subscribers
local SubscribersValue = player.leaderstats.Subscribers.Value
local success, errormessage = pcall(function()
SubscribersDataStore:SetAsync(playerUserId, SubscribersValue)
end)
end)
game:BindToClose(function(player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..player.UserId
-- Saving Followers
local FollowersValue = player.leaderstats.Followers.Value
local success, errormessage = pcall(function()
FollowersDataStore:SetAsync(playerUserId, FollowersValue)
end)
-- Saving Subscribers
local SubscribersValue = player.leaderstats.Subscribers.Value
local success, errormessage = pcall(function()
SubscribersDataStore:SetAsync(playerUserId, SubscribersValue)
end)
end
end)
I went through and improved certain areas of your code to ensure that it works correctly (and to the most efficient way that I can possibly make it from my rather limited knowledge):
local dataStoreService = game:GetService("DataStoreService")
local DataStore = dataStoreService:GetDataStore("Followers")
local debounce = true
game.Players.PlayerAdded:Connect(function(player)
local StreamStats = Instance.new("Folder") -- Creating StreamStats folder inside player
StreamStats.Parent = player
StreamStats.Name = "StreamStats"
local Followers = Instance.new("IntValue") -- Creating intValue inside leaderstats with name Followers
Followers.Parent = StreamStats
Followers.Name = "Followers"
--Followers.Value = 0
local Subscribers = Instance.new("IntValue") -- Creating intValue inside leaderstats with name Subscribers
Subscribers.Parent = StreamStats
Subscribers.Name = "Subscribers"
--Subscribers.Value = 0
local key = "player_"..player.UserId
-- Loading Followers Data
local Data = DataStore:GetAsync(key)
if Data then -- this could be automated using a loop but in this state, any new values that you add must be placed in here manually
Followers.Value = Data[1]
Subscribers.Value = Data[2]
local good, bad = pcall(function()
DataStore:SetAsync(key, Data)
print("DataStore for "..player.Name.." is set.")
end)
if good then
print(good)
else
print(bad)
end
else
local items = { -- this could be automated using a loop but in this state, any new values that you add must be placed in here manually
player.StreamStats.Followers.Value,
player.StreamStats.Subscribers.Value,
}
DataStore:SetAsync(key, items)
print("DataStore for "..player.Name.." is set.")
end
end)
-- Saving Data
game.Players.PlayerRemoving:Connect(function(player)
task.wait()
if debounce == true then
warn("Debug: PlayerRemoving running")
local itemsToSave = {}
for _, item in ipairs(player.StreamStats:GetChildren()) do -- runs a loop to add the values into the itemsToSave table
if item:IsA("IntValue") or item:IsA("NumberValue") or item:IsA("StringValue") or item:IsA("BoolValue") then -- you can remove/modify this if necessary; this just filters out anything that may not contain a value to save
table.insert(itemsToSave, item.Value) -- adds the value to the table
end
end
local key = "player_"..player.UserId
local success, errormessage = pcall(function()
DataStore:SetAsync(key, itemsToSave)
end)
if success then
print(success)
else
print(errormessage)
end
else
return --just in case
end
end)
game:BindToClose(function()
debounce = false
warn("Debug: BindToClose running")
local plrs = game.Players:GetPlayers()
for _, Player in pairs(plrs) do
if Player:IsA("Player") then
local itemsToSave = {}
for _, item in ipairs(Player.StreamStats:GetChildren()) do -- runs a loop to add the values into the itemsToSave table
if item:IsA("IntValue") or item:IsA("NumberValue") or item:IsA("StringValue") or item:IsA("BoolValue") then -- you can remove/modify this if necessary; this just filters out anything that may not contain a value to save
table.insert(itemsToSave, item.Value) -- adds the value to the table
end
end
local key = "player_"..Player.UserId
local success, errormessage = pcall(function()
DataStore:SetAsync(key, itemsToSave)
end)
if success then
print(success)
else
print(errormessage)
end
end
end
end)
Of course, this is just a base design that has had very little testing done to it to ensure that it does function decently. Deciding whether to use game.BindToClose() or game.Players.PlayerRemoving() is done using a very cheap (but relatively reliable) debounce.
One thing I want to point out in this script is that it doesn’t save consistently in Studio, but does in a regular server. I’m not entirely sure why… If anyone does know, please lmk