There are two CoreScripts in particular that rely on bad practices to function as intended. This has drastic side effects and consequences in our games.
Both of the following scripts rely on polling an instance for the expected instance. This behavior needs to be adjusted to rely on events to prevent the current high memory use and performance costs from happening.
VehicleHud.lua
This script relies on a while loop that searches the children of the character to find the humanoid. Instead this should be calling character:FindFirstChildWhichIsA("Humanoid")
and if it doesn’t exist, use character.DescendantAdded()
to wait for it. This polling behavior causes frame drops in games that have characters with no humanoid and hundreds of parts.
local function getHumanoid()
local character = LocalPlayer and LocalPlayer.Character
if character then
for _,child in pairs(character:GetChildren()) do
if child:IsA('Humanoid') then
return child
end
end
end
end
local function connectSeated()
local humanoid = getHumanoid()
while not humanoid do
wait()
humanoid = getHumanoid()
end
humanoid.Seated:connect(onSeated)
end
ServerLeaderstats.Lua
This script loops through all children of player, and all the children of all of the top level instances, and yields infinitely until object gets removed. There are better methods available to wait for an instance to be removed, but it doesn’t even seem like this code needs to be listening to all instances inside of the player. These yields build up over time as there are more instances under player, leading to high memory usage.
local function playerChildAdded(instance)
if not isValidContainer(instance) then
return
end
local nameChanged = instance:GetPropertyChangedSignal("Name"):Connect(function ()
playerChildNameChanged(instance)
end)
fastSpawn(playerChildNameChanged, instance)
waitUntilRemoved(instance)
nameChanged:Disconnect()
if instance.Name == LEADERSTATS_NAME then
leaderstatsRemoved(instance)
end
end
local function playerAdded(player)
local childAdded = player.ChildAdded:Connect(playerChildAdded)
for _, instance in ipairs(player:GetChildren()) do
fastSpawn(playerChildAdded, instance)
end
waitUntilRemoved(player)
childAdded:Disconnect()
end