So, the server doesn’t print both of my test account in studio mode when I leave the game, they print like a few seconds after… This is kinda strange when when I have a queue for it. Help needed please
local Scope = "Version1"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Stats", Scope)
local Event = game:GetService("ReplicatedStorage"):FindFirstChild("Events"):FindFirstChild("DATASTORE_UPDATE")
local PlayerService = game:GetService("Players")
local Main_Folder = Instance.new("Folder")
Main_Folder.Name = "DataStore"
Main_Folder.Parent = game:GetService("ServerScriptService")
-- For datastore
local Queue = {}
local Cooldown = 6
local Debounce = false
local function Save()
for e, v in pairs(Queue) do
local succ, err = pcall(function()
DataStore:SetAsync(e, {
["Total Wins"] = v["Total Wins"],
["Total Deaths"] = v["Total Deaths"],
["Best Killstreak"] = v["Total Killstreak"]
})
end)
if succ then
print("Saved data for", e)
else
print(err)
end
end
Queue = {}
end
PlayerService.PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Configuration")
Folder.Name = Player.Name
Folder.Parent = Main_Folder
local Wins_Folder = Instance.new("IntValue")
Wins_Folder.Name = "Total Wins"
Wins_Folder.Parent = Folder
local Deaths_Folder = Instance.new("IntValue")
Deaths_Folder.Name = "Total Deaths"
Deaths_Folder.Parent = Folder
local Best_Killstreak_Folder = Instance.new("IntValue")
Best_Killstreak_Folder.Name = "Best Killstreak"
Best_Killstreak_Folder.Parent = Folder
local SSS = game:GetService("ServerScriptService")
local Folder = SSS:FindFirstChild("DataStore")
wait(Cooldown)
--[[ > > > Loading stats < < < ]]--
local succ, err = pcall(function()
local Score = DataStore:GetAsync(Player.UserId) or {
0,
0,
0
}
Wins_Folder.Value = Score["Total Wins"]
Deaths_Folder.Value = Score["Total Deaths"]
Best_Killstreak_Folder.Value = Score["Best Killstreak"]
end)
if succ then
print("Loaded data for", Player.UserId)
else
print("Failed loading data for", Player.UserId, ":", err)
end
Event:FireClient(Player, Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Wins").Value, "WINS")
Event:FireClient(Player, Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Deaths").Value, "DEATHS")
Event:FireClient(Player, Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Best Killstreak").Value, "KILLSTREAK")
--[[ > > > Update KD when player joins < < < ]]--
local Update_Wins = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Wins")
local Update_Deaths = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Deaths")
local updatekd = Update_Wins.Value / (Update_Deaths.Value == 0 and 1 or Update_Deaths.Value)
Event:FireClient(Player, math.floor(updatekd * 100 + 0.5) / 100, "K/D RATIO")
--[[ > > > Get their stats < < < ]]--
local Wins_Changed = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Wins")
Wins_Changed:GetPropertyChangedSignal("Value"):Connect(function()
Event:FireClient(Player, Wins_Changed.Value, "WINS")
local Update = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Wins").Value
local kd = Update / (Wins_Changed.Value == 0 and 1 or Wins_Changed.Value)
Event:FireClient(Player, math.floor(kd * 100 + 0.5) / 100, "K/D RATIO")
end)
local Deaths_Changed = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Deaths")
Deaths_Changed:GetPropertyChangedSignal("Value"):Connect(function()
Event:FireClient(Player, Deaths_Changed.Value, "DEATHS")
local Update = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Total Wins").Value
local kd = Update / (Deaths_Changed.Value == 0 and 1 or Deaths_Changed.Value)
Event:FireClient(Player, math.floor(kd * 100 + 0.5) / 100, "K/D RATIO")
end)
local Best_Killstreak_Changed = Main_Folder:FindFirstChild(Player.Name):FindFirstChild("Best Killstreak")
Best_Killstreak_Changed:GetPropertyChangedSignal("Value"):Connect(function()
Event:FireClient(Player, Best_Killstreak_Changed.Value, "KILLSTREAK")
end)
--[[ > > > Save when player leaves < < < ]]--
PlayerService.PlayerRemoving:Connect(function(Player)
Queue[Player.UserId] = {
["Total Wins"] = Wins_Folder.Value,
["Total Deaths"] = Deaths_Folder.Value,
["Best Killstreak"] = Best_Killstreak_Folder.Value
}
while Debounce do
wait(1)
end
Debounce = true
Save()
wait(Cooldown)
Debounce = false
end)
--[[ > > > Save if shutdown occurs < < < ]]--
game:BindToClose(function()
for _,v in pairs(PlayerService:GetPlayers()) do
Queue[v.UserId] = {
["Total Wins"] = Wins_Folder.Value,
["Total Deaths"] = Deaths_Folder.Value,
["Best Killstreak"] = Best_Killstreak_Folder.Value
}
while Debounce do
wait(1)
end
Debounce = true
Save()
wait(Cooldown)
Debounce = false
end
end)
end)