Script running beyond allowed execution time

Hello! Everytime I close my game, I am stuck in studio for so long and I’m pretty sure it’s because of my AI scripts. How would I be able to fix their loops? Thanks! Code:

Update: please look at my post, this is the current issue: errors with no source

1 Like

These scripts are quite concerning especially the second one. The fact that there is no delay inside the while true loop is a red flag.

Although rewriting everything would be better, the current issue is likely caused by the following code segment.

repeat
	task.wait()
until Agent.Idle == true or isRunning == true

Try checking the values of Agent.Idle and isRunning inside the repeat until loop.

I’m assuming by rework you mean to minimize my usage of while loops. I do agree, but I’m focused on other things in my project. I have changed the code snippit to the following, however I don’t believe it’s the right way to do it… (If it is, correct me if I’m wrong)

repeat
	if Agent.Idle == true or isRunning == true then
		break
	end
until task.wait()

Oh that seemed to be the issue! I’ll be more aware of this in the future, as I am not too good with while or repeat loops :sweat_smile:

Update: The issue seems to be on and off, not sure what’s causing it 100%

So update, the error I’m getting now is no longer sourced to a script. Any ideas?

Please send some scripts you think might be causing this; otherwise there is no way to help you find the problem

1 Like

Im commenting out some code, and I think I have found the culprit. Give me a second to do some testing and I’ll send the results.

function updatePlayerHealth(plrname)
    local player = game.Players:FindFirstChild(plrname)
	for i,v:Instance in player.Limbs:GetChildren() do
		if not v:IsA("IntValue") then continue end
		-- do stuff
	end
	if player.MainHealth.Value > 100 then
		player.MainHealth.Value = 100
	end
	if player.MainHealth.Value <= 0 then
		player.Character.Humanoid.Health = 0
	end
end
game["Run Service"].Heartbeat:Connect(function(dt)
	for i,v in game.Players:GetPlayers() do
		updatePlayerHealth(v.Name)
	end
end)

This is causing the game to freeze after closing… Not sure how I would change it considering this stuff needs to run very often.

1 Like

Don’t know what the “-- do stuff” part is for, but here is a new version of your script that is more efficient, as it clamps MainHealth using GetPropertyChangedSignal instead of updating it on heartbeat

local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")

local playersThatHaveTheirHealthClamped = {}

local maxHealth = 100
local minHealth = 0

local function clampHealth(player : Player)
	if playersThatHaveTheirHealthClamped[player] then
		return
	end
	
	playersThatHaveTheirHealthClamped[player] = true
	
	local healthValueInstance = player:WaitForChild("MainHealth") :: NumberValue | IntValue
	
	healthValueInstance:GetPropertyChangedSignal("Value"):Connect(function()
		healthValueInstance.Value = math.clamp(healthValueInstance.Value, minHealth, maxHealth)
	end)
end

local function onHeartbeat()
	for _, player in PlayerService:GetPlayers() do
		clampHealth(player)

		for i, v in player.Limbs:GetChildren() :: {Instance} do
			if v:IsA("IntValue") then
				--do stuff
			end
		end
	end
end

RunService.Heartbeat:Connect(onHeartbeat)
1 Like

--do stuff is to hide code, however wouldn’t it be more efficient if instead the clampHealth would be under a PlayerAdded listener? Anywho, that seemed to have made it a lot faster! This issue does come and go though so I hope this is a final fix. Thanks for your help.

(I’ll mark as solution but if this probelm arrises again, I will create a new post)

1 Like

Yes it would, I just wanted to keep your loop logic but your observation is totally correct!
If it still crashes sometimes, then its definitely related to the hidden code you commented as “do stuff”

1 Like

Although I do not know the specifics, I doubt it is necessary. You should try to minimize the stuff you run every frame as it will affect your game performance. Instead of every frame, update the health when you know it changes (i.e. the player takes damage).

1 Like

I’ll keep that in mind! I’ll probably add an event to take damage or whatever instead of heartbeat in the future.

1 Like