Wait loop causing crashing

In my game, after about 20 minutes of playing, the game will do a hard crash (no disconnect, just freezes and you have to close the game through task manager)

I believe it is because of the wait loops in my game

I have set an extra wait inside the wait loop but I dont know if I did it right since the game is still crashing, here’s an example:

local plr = game.Players.LocalPlayer
local runes = plr:WaitForChild("Data"):WaitForChild("Runes")
local stat = "Elite"
local runestat = runes:WaitForChild(stat)

while task.wait() do
	script.Parent.Text = runestat.Value
	wait(1)
end
2 Likes

There is no issue with this code

so then why is it crashing? I made sure every script that had a wait loop has this, but it still crashes

Impossible to say with the information I have :confused:

1 Like

Do
while true do
(your code)
task.wait(1)
end

2 Likes

Stuff like these need debugging with print statement but it is not the while loops fault, maybe another script is causing it

1 Like

Hey!

I don’t really know why it is crashing. In times like these, it is best to use RunService’s Heartbeat function, which will run based off of memory usage and stuff like that.

Try this code:

local plr = game.Players.LocalPlayer
local runes = plr:WaitForChild("Data"):WaitForChild("Runes")
local stat = "Elite"
local runestat = runes:WaitForChild(stat)

local function updateText()
    script.Parent.Text = runestat.Value
end

-- Connect to Heartbeat event to update the text every second
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
    local timer = 0
    while timer < 1 do
        timer = timer + deltaTime
        wait()
    end
    updateText()
end)

Also yeah this was made using ChatGPT don’t give me credit lol. If this script doesn’t work, as other people said, it is probably because of another script.

would this cause crashing? its for my datastore

while task.wait(240) do
	for _, plr in (Players:GetChildren()) do
		if not plr:FindFirstChild("Loaded") or not plr.Loaded.Value then return end -- player is not loaded

		pcall(function()
			Datastore.SaveData(plr, true) -- save
		end)
		plr.Loaded.Value = true -- set loaded true as it was an autosave
	end
end

No it wouldn’t crash the server (unless there are like 5000+ people playing in the server)

If the game crashes after 20 minutes, my guess would be that you have a memory leak, causing the client’s (or server’s) memory usage to slowly creep up until it crashes. You might want to verify the memory usage on the client (if the client is the one crashing) or the server.

There is also nothing wrong with that loop (though using wait twice is ummm weird, and you should never use wait(), it should be deprecated tbh. I assume it’s there because you were testing different things)

What??? RunService.Heatbeat does not care about memory. RunService.Heartbeat:Wait() is actually the same as task.wait()

I dont know too much about memory leaks, I monitored the dev console from when I joined to when it crashed

16:41 - 1600 MB
16:42 - 1640 MB
16:43 - 1643 MB
17:00 - Crash (1649MB)

That is quite insignificant, so I’d probably rule that out. There is an increase of 49MB which isn’t a lot, and 1.6GB of ram is quite normal as well

Do you experience any lag or something before it crashes? If not then, maybe it’s another script causing the crash.

Do other players also experience this issue?

no I dont experience any lag at all, and yes other players have experienced the crashing, it was happening in one of my other games as well, which this game uses a few scripts from

Could you share some of the other scripts that are in both games? If you are not experiencing lag before it happens, then it makes me think it’s an issue with a script that freeze the game in a specific frame (such as the while loop you suspected or recursive function calls that never end)

Or it could be something else entirely…

In studio, there is a maximum execution time, that will cause an error if exceeded, I am not sure if that is also in place in live games

The fact it happens at 20 minutes after you join is also something worth investigating (especially if it’s also ~20 minutes for other players). If it is caused by a never ending thread, then that would mean that situation triggers at that precise time

I have these group rewards

local Players = game:GetService("Players")
local GroupId = 33852318 --your group id
local x = require(game.ReplicatedStorage.Modules.EternityNum)

Players.PlayerAdded:Connect(function(Player)
	if Player:WaitForChild("Data"):WaitForChild("PlayerData"):WaitForChild("IsInGroup").Value == true then
		return
	end
	if(Player:IsInGroup(GroupId)) then
		local s = Player:WaitForChild("Data"):WaitForChild("Stats")
		local m = Player:WaitForChild("Data"):WaitForChild("Runes"):WaitForChild("GroupMember")
		m.Value = 1
		Player:WaitForChild("Data"):WaitForChild("PlayerData"):WaitForChild("Luck").Value += 1
		Player:WaitForChild("Data"):WaitForChild("PlayerData"):WaitForChild("IsInGroup").Value = true
	end
end)

Gamepass

local MPS = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(plr)
	local s = plr:WaitForChild("Data"):WaitForChild("PlayerData")
	if s:WaitForChild("HasLuck").Value == true then
		return
	end
	if MPS:UserOwnsGamePassAsync(plr.UserId, 718540314) then
		s:WaitForChild("RobuxSpent").Value += 99
		s:WaitForChild("Luck").Value += 1
		s:WaitForChild("HasLuck").Value = true
	end
end)

Join badge

warn("["..script.Name.."] has loaded")

local BadgeID = 89144405299239 

game.Players.PlayerAdded:Connect(function(plr)
	local DefaultNum = 00000000  
	if BadgeID == DefaultNum then 
		error("Forgot to add badge ID (line 26)")
	elseif DefaultNum == BadgeID then
		error("Do not change \"DefaultNum\" to your ID (Line 33)")
	elseif BadgeID == nil then
		error("Badge ID doesn't exist or is nil (line 26)")
	end
	print("Giving BadgeID: " .. BadgeID .. " to: " .. plr.Name)
	local b = game:GetService("BadgeService")
	b:AwardBadge(plr.userId, BadgeID)
end)

Text changer

local label = script.Parent.Frame.TextLabel

while true do
	label.Text = "Join the group for +1 Rune Luck, and a +x3 Multiplier boost!"
	task.wait(10)
	label.Text = "Join our communications server linked below for changelogs, sneakpeeks, and more!"
	task.wait(10)
	label.Text = "Open Runes to boost your stats even more!"
	task.wait(10)
	label.Text = "Enjoying the game? Like and Favorite the game!"
	task.wait(10)
end

Those are the only ones I can think of that could cause problems, I have other scripts in here that are used between both games but I gave them to some friends to use in their games and they are working just fine with no crashing

None of these are problematic…

What is EternityNum?

1 Like

a module used for number shortening since I am developing an incremental game it is necessary for when the numbers exceed robloxs limit

could it be this player collisions disabler? it turns up that setpartcollisiongroup is deprecated idk if that would cause issues or not

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
 
local playerCollisionGroupName = "Players"
PhysicsService:CreateCollisionGroup(playerCollisionGroupName)
PhysicsService:CollisionGroupSetCollidable(playerCollisionGroupName, playerCollisionGroupName, false)
 
local previousCollisionGroups = {}
 
local function setCollisionGroup(object)
  if object:IsA("BasePart") then
    previousCollisionGroups[object] = object.CollisionGroupId
    PhysicsService:SetPartCollisionGroup(object, playerCollisionGroupName)
  end
end
 
local function setCollisionGroupRecursive(object)
  setCollisionGroup(object)
 
  for _, child in ipairs(object:GetChildren()) do
    setCollisionGroupRecursive(child)
  end
end
 
local function resetCollisionGroup(object)
  local previousCollisionGroupId = previousCollisionGroups[object]
  if not previousCollisionGroupId then return end 
 
  local previousCollisionGroupName = PhysicsService:GetCollisionGroupName(previousCollisionGroupId)
  if not previousCollisionGroupName then return end
 
  PhysicsService:SetPartCollisionGroup(object, previousCollisionGroupName)
  previousCollisionGroups[object] = nil
end
 
local function onCharacterAdded(character)
  setCollisionGroupRecursive(character)
 
  character.DescendantAdded:Connect(setCollisionGroup)
  character.DescendantRemoving:Connect(resetCollisionGroup)
end
 
local function onPlayerAdded(player)
  player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

The deprecated stuff should not be an issue. There is the setCollisionGroupRecursive that is well, recursive, but I don’t see how it can run forever, so very unlikely to be the issue

You might be able to see the error and where it’s from if you wait for it to happen in studio. When play testing in studio, it throws an error when the execution time exceeds 10 seconds