Hey! So I gogt this code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Level2")
local rep = game:FindFirstChild("ReplicatedStorage")
local mps = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
if player.UserId == 1430026986 or player.UserId == 1071071938 or player.UserId == 3119824160 then
local changepart = rep:FindFirstChild("ChangeEmployeePart"):Clone()
changepart.Parent = player.PlayerGui
end
---DATA STUFF
--acount age req
--if player.AccountAge < 7 then player:Kick("Your account is not old enough to play this game. Required account age (days): 7, current account age(days): " ..player.AccountAge) end
local PlayerData = game.ServerStorage.PlayerData:Clone()
PlayerData.Parent = player
local leaderstats = game.ServerStorage.leaderstats:Clone()
leaderstats.Parent = player
task.wait(1)
coroutine.wrap(function()
local Success, data = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
if Success then
if data then----IF DATA----IF DATA----IF DATA----IF DATA----IF DATA----IF DATA----IF DATA
--print(data)
--UPDATE DATA TO THE VALUES
if data.Playtime then PlayerData.Playtime.Value = data.Playtime end
if data.Level then PlayerData.Level.Value = data.Level leaderstats.Level.Value = data.Level end
if data.XP then PlayerData.XP.Value = data.XP end
local aprooval = Instance.new("BoolValue")
aprooval.Name = "SaveAprooval"
aprooval.Parent = PlayerData
else----IF HAS NO DATA----IF HAS NO DATA----IF HAS NO DATA----IF HAS NO DATA----IF HAS NO DATA
warn("Wasnt able to gather the data")
task.wait(1)
local aprooval = Instance.new("BoolValue")
aprooval.Name = "SaveAprooval"
aprooval.Parent = PlayerData
PlayerData.Level.Value = 2
PlayerData.XP.Value = 2
task.wait(1)
PlayerData.Level.Value = 1
PlayerData.Level.Value = 1
end
else
warn("Data filed to load")
--player:Kick("Data failed to load, please rejoin the game.")
end
end)()
end)
--SAVING
local function Save(player)
if not player:FindFirstChild("PlayerData") then warn("ATTEMPT TO SAVE DATA BUT DIDNT FIND DATA FOLDER") return end
if not player.PlayerData:FindFirstChild("SaveAprooval") then print("ATTEMPT TO SAVE DATA BUT DIDNT FIND APPROOVAL") return end
local newData = {
["Playtime"] = player.PlayerData.Playtime.Value,
["Level"] = player.PlayerData.Level.Value,
["XP"] = player.PlayerData.XP.Value,
}
local Success, data = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
--print(data)
--print(newData)
if data ~= nil then
if data.Playtime ~= nil then
if newData.Playtime < data.Playtime then
print("ATTEMPT TO SAVE DATA BUT PLAYTIME WAS LOWER")
return
end
end
end
--print("DATA SAVED")
DataStore:SetAsync(player.UserId,newData --[[function(data) return newData end--]]) -- BEFORE UPDATE ASYNC
end
game.Players.PlayerRemoving:Connect(function(plr)
coroutine.wrap(function()
Save(plr)
end)()
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
Save(player)
end
end)
local Counter = 0
while task.wait(15) do
warn("Still running the task.wait 15")
Counter += 15
for i,v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild("PlayerData") ~= nil and v:FindFirstChild("leaderstats") ~= nil then
v.PlayerData.Playtime.Value += 15
if Counter >= 90 then
if v.PlayerData:FindFirstChild("XP") ~= nil and v.PlayerData:FindFirstChild("Level") ~= nil then
if mps:UserOwnsGamePassAsync(v.userId, 189517075) then
if game.PrivateServerId ~= "" and game.PrivateServerOwnerId ~= 0 then else
if v.MembershipType == Enum.MembershipType.Premium then
v.Player.XP.Value += 600
else
v.PlayerData.XP.Value += 400
end
end
else
if game.PrivateServerId ~= "" and game.PrivateServerOwnerId ~= 0 then else
if v.MembershipType == Enum.MembershipType.Premium then
v.PlayerData.XP.Value += 300
else
v.PlayerData.XP.Value += 200
end
end
end
if v.PlayerData.XP.Value >= (v.PlayerData.Level.Value) * 100 then
v.PlayerData.Level.Value += 1
v.leaderstats.Level.Value = v.PlayerData.Level.Value
if v.Character then
if v.Character.Head:FindFirstChild("GroupRank") then
if v:FindFirstChild("ResetChatTag") then else
v.Character.Head.GroupRank.Level.Text = "<u>Level "..v.PlayerData.Level.Value.."</u>"
end
end
end
v.PlayerData.XP.Value = 0
end
end
end
end
end
if Counter >= 90 then Counter = 0 end
end
Randomly, this whole loop simply breaks. So, we are getting reports from our player base that XP randomly “freeze” till a new server is being created. I joined such server and checked the server logs and it’s not pringing the “Still running the task.wait 15” meaning, the loop must break but I can’t find why. Anyone who could help me with this?