Hey! So I got this script:
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 not (v:FindFirstChild("PlayerData") and v:FindFirstChild("leaderstats")) then
continue
end
v.PlayerData.Playtime.Value += 15
if Counter < 90 then
continue
end
if not (v.PlayerData:FindFirstChild("XP") and v.PlayerData:FindFirstChild("Level")) then
continue
end
print("LevelData : Gotten too (v.PlayerData:FindFirstChild('XP'))")
if (game.PrivateServerId == "" and game.PrivateServerOwnerId == 0) then
local baseXP = (if (v.MembershipType == Enum.MembershipType.Premium) then 150 else 100)
local passMult = (if mps:UserOwnsGamePassAsync(v.userId, 189517075) then 2 else 1)
v.PlayerData.XP.Value += (baseXP * passMult)
print("Awarded "..v.Name.." with "..(baseXP * passMult).." XP.")
end
if v.PlayerData.XP.Value < (v.PlayerData.Level.Value) * 100 then
continue
end
v.PlayerData.Level.Value += 1
v.leaderstats.Level.Value = v.PlayerData.Level.Value
local character = v.Character
if (character and character.Head:FindFirstChild("GroupRank")) then
if not (v:FindFirstChild("ResetChatTag")) then
v.Character.Head.GroupRank.Level.Text = "<u>Level "..v.PlayerData.Level.Value.."</u>"
end
end
v.PlayerData.XP.Value = 0
end
if Counter >= 90 then Counter = 0 end
end
and it randomly stops. Like, the whole loop. It won’t print: “Still running the task.wait 15”. I am sadly not able to provide an error because it’s happening randomly in servers and till then the error is out of the console. Every help is appreciate!
- Try Using pcall: Put your code inside a pcall function can help catch any errors that might be causing the script to stop. Here’s an example:
local success, message = pcall(function(), message = pcall(function()
-- u can put ur code here
end)
if not success then
print("Error: " .. message)
end
this should print out any errors that occur within the pcall function, which might help you or us identify the issue.
This wouldn’t help verry much because this is in a game with quite some players. We are printing out a few things and when it’s getting reported and I join first then, the error from the pcall function would be away.
Do you have any idea where the problem in the code would be?
I’m sorry to hear this bro but its hella hard for me to identify the issue but l’ll continue to find the issue
I know that’s it’s hard which is why I am posting this here. Like, I looked throught this chunk of code and there are like no issue but there must be. 
Try to change the task.wait(15) to true and put the task.wait on the loop. But if I can suggest a thing, stop using a loop like this to level up your players.
Try using the instance with the name “xp” and its GetPropertyChangedSignal to determine when to check if the player needs a level up.
game.Players.PlayerAdded:Connect(function(player)
-- Your existing code with leaderstats...
local XP = Instance.new("IntValue")
XP.Name = "xp"
XP.Parent = leaderstats
XP:GetPropertyChangedSignal("Value"):Connect(function() -- Use the instance with the name 'xp' and its GetPropertyChangedSignal
-- Level up verification code here
end)
end)
Like that you will be sure not to have an error that could potentially stop your loop and it would be more efficient in my opinion.
If you have questions, don’t hesitate to ask me!
Heyy! Tysm but the loop is also awarding the XP in order so they can level up.
I’ll try it out with the task.wait(15) to true. Shall I just do:
while true do
task.wait(15)
...
...
...
??
Yes, that’s what you should do. If task.wait returns nil for some obscure reason, it will break your loop. By using true, you ensure that this will never happen. I understand that your loop is giving XP, and you don’t want to get rid of it. However, if you tell me what game you’re trying to make, I might be able to give you advice on how to find a new logic for giving XP to your players. Also, using pcall as suggested by RadAnimeBro could also help prevent the loop from stopping for a single error.
Alrighty, thank you! I will try it definently out. 