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
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()
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.
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)
--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)
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”
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).