Memory is through the roof. What should I change?

Module Script:

local module = {}

function module.NightisArriving()
	local interval = .025
	local change = .025
	
	while wait(interval) do
		game.Lighting.ClockTime =  game.Lighting.ClockTime + change
		if game.Lighting:GetMinutesAfterMidnight() > 22 * 60  then
			break
		end
	end
end

function module.WallsCanCollide(status)
	for i, v in pairs(workspace.Game.InvisibleWalls:GetChildren()) do
		v.CanCollide = status
	end
end

function module.MakeMusicStatus(start, End, increase)
	for loop = start, End, increase do
		workspace.Audio.ForestTheme.Volume = loop
		wait(.05)
	end
end

function module.Crickets(start, End, increase)
	for loop = start, End, increase do
		workspace.Audio.Crickets.Volume = loop
		wait(.05)
	end
end

function module.SmilingKidsParent(whatIs, parent)
	local SmilingKidWithRope = whatIs
	SmilingKidWithRope.Parent = parent
end

function module.SmilingKidFalls()
	while true do
		for i = 1, 100 do
			wait()
			workspace.SmilingKidWithRope:SetPrimaryPartCFrame(CFrame.new(workspace.SmilingKidWithRope.PrimaryPart.Position + Vector3.new(0,-1,0)))
		end
		break
	end
end

function module.Fire(Status, Vol, length)
	workspace.Game.Campfire.Campfire.FirePart.ParticleEmitter.Enabled = Status
	local TweenService = game:GetService("TweenService")
	local TweenInf = TweenInfo.new(length, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0)
	local goals = Vol
	
	local ScreenBlack = TweenService:Create(workspace.Game.Campfire.Campfire.FirePart.FireCrackles, TweenInf,{Volume = goals})
	ScreenBlack:Play()
end

function module.RunMusic(Volume, Status, TimePosition)
	workspace.Audio["RUN!"].Volume = Volume
	workspace.Audio["RUN!"].Playing = Status
	workspace.Audio["RUN!"].TimePosition = TimePosition
end

function module.ScreenTransparency(Status)
	game.ReplicatedStorage.RemoteEvents.MakeScreenBlack:FireAllClients(Status)
end

function module.DistanceFromKill()
	local nearestEnemy = nil
	local nearestRoot = math.huge 
	workspace.SmilingKidWithRope.SmilingKid.Parent = workspace
	local root = workspace.SmilingKid.PrimaryPart
	for i, v in pairs(workspace.Game.People:GetChildren()) do 
		local plrRoot = v.PrimaryPart
		
		if plrRoot and plrRoot ~= root then
			local mag = (plrRoot.Position - root.Position).Magnitude
			if mag < 50 and mag < nearestRoot then
				nearestEnemy = plrRoot
				nearestRoot = mag
			end
		end
		if nearestEnemy then
			local newpos = nearestEnemy.Position
			local lookvector = nearestEnemy.CFrame.lookVector
			workspace.SmilingKid:SetPrimaryPartCFrame(CFrame.new(newpos - lookvector * 3, newpos + lookvector))
			plrRoot = plrRoot.Parent.Humanoid
			plrRoot.WalkSpeed = 0
			plrRoot.JumpPower = 0
			wait(3)
			plrRoot.Health = 0 
		end 
	end
end

function module.Raining(status)
	for i, Part in pairs(workspace.Game.Rain:GetChildren()) do
		for i, Seat in pairs(Part:GetChildren()) do
			Seat.Enabled = status
		end
	end
end

function module.GetRidOfRope()
	workspace.SmilingKidWithRope.Parent = game.ServerStorage
end

function module.TelePlayers(Pos)
	game.ReplicatedStorage.RemoteEvents.TelePlayers:FireAllClients(Pos)
end

function module.HumanoidStatus(sit, wspeed, jump)
	game.ReplicatedStorage.RemoteEvents.Stats:FireAllClients(sit, wspeed, jump)
end

function module.Fog(FogStart, FogEnd)
	game.ReplicatedStorage.RemoteEvents.Fog:FireAllClients(FogStart, FogEnd)
end
	
return module

Remote Events:

--Services and locals
local plr = game.Players.LocalPlayer
local char = plr.Character
local HRP = char:WaitForChild("HumanoidRootPart")
local H = char:WaitForChild("Humanoid")
local PlayerGui = plr:WaitForChild("PlayerGui")
local TweenService = game:GetService("TweenService")
local BOF = PlayerGui.UI.BlackedOutFrame
local Rem_Events = game.ReplicatedStorage.RemoteEvents

--Remote Events
Rem_Events.MakeScreenBlack.OnClientEvent:Connect(function(Status)
	local TweenInf = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0)
	local goals = Status
	
	local ScreenBlack = TweenService:Create(BOF, TweenInf,{Transparency = goals})
	ScreenBlack:Play()
end)

Rem_Events.TelePlayers.OnClientEvent:Connect(function(Position)
	HRP.CFrame = (Position)
end)

Rem_Events.Stats.OnClientEvent:Connect(function(SitStats, wspeed, jump)
	H.Sit = SitStats
	H.WalkSpeed = wspeed
	H.JumpPower = jump
end)

Rem_Events.Fog.OnClientEvent:Connect(function(FogStart, FogEnd)
	game.Lighting.FogStart = FogStart
	game.Lighting.FogEnd = FogEnd
end)

What should I change in my code to make the memory go down? Should I disconnect function? Is that all? The code works and all, just itā€™s not the best on memory.

1 Like

This is absolubtly dumb.
Making a for loop and breaking it to run once?
Then donā€™t do loop /:

So what should I specifically change? Also, Iā€™m new to the business if you havenā€™t already noticed. So Iā€™m gonna do some dumb stuff before I do smart stuff.

Edit: Get rid of while loop??? Also most of this code was written a bit ago. Iā€™ve learned more things since then.

1 Like

Memory issues come from many different places within a game.
To determine it is in fact your code, please press F9 go to the Scripts section.
Send a screenshot of where you see your server script activities. General rule of thumb is that anything over 3% needs looking into.

1 Like

The StoryScript(server script, didnā€™t sent it though.) isnā€™t hitting high numbers at all. But ā€˜Mainā€™ is hitting 4% - 5% consistently. What is main? Is that on the client or something else?

Those should be the name of your scripts. Whatever ā€œMainā€ is doing is somewhat intensive work.
I also advise checking out the ā€œMemory Management Tipsā€ here to further optimize your game.

1 Like

I canā€™t find Main anywhere. I even searched it up and nothing pops up for me. Do you have any idea what this means? Maybe main is all the Server Script or something. Plus, thereā€™s no way to differ between Client and Server in the Scripts section.

Could you show me a screenshot of it?
Also, my apologies. Client script information is not included there. Are you feeling a lot of work is being done there?

I figured out that itā€™s happening with the Server and the Client. They both have the same memory issues. I would screenshot, but it goes to my clipboard. Then I try to send it to the DevForum but I cant find the saved screenshot anywhere.

@megukoo I searched up main again, but this time I found ā€˜ChatMain.ā€™ Itā€™s a local script inside of Chat that enables bubble chat. Could this be it? I personally donā€™t think so, but itā€™s possible.

Shouldnā€™t be ChatMain.
Since you donā€™t seem to be able to pinpoint the script, Iā€™d suggest raising some of the intervals in the module script (to say 1 second.) See how it affects performance then.

If you join then start lagging over time then you have memory leaks, this could be due to something like this:

while true do wait()
    local newvariable = true
end

You gotta destroy the reference because millions of references will exist

newvariable = nil

if you just lag from the start then either it is your computer orā€¦
you have viruses or a ton of data in the game

Thatā€™s not quite how it works. When the code reaches where the end would be, the reference to the variable would be gone. Manually assigning var = nil is not necessary when the scope will be terminated.

:thinking: then how does memory leaks work, would it just be events that donā€™t disconnect. Thanks for the new information.

Events and such are handled by Roblox. An event canā€™t be garbage collected while itā€™s still connected, but an event can only be disconnected by you (or calling :Destroy() on the object).

Memory leaks in Lua might happen, for example, when thereā€™s a table used in an event that keeps being added into. Since the event wonā€™t GC, and the table will stick around for as long as the event does, any new items added to the table without previous being removed will add to the memory footprint.

There are many ways, but in general you wonā€™t find the issue from variables that get used and thrown around in temporary scopes. Also, booleans, numbers and nil donā€™t allocate memory in the sense strings or tables do.

1 Like

I have some function modules. Does that take up some memory? Also, how would I connect a function that I can reuse without taking much memory?

function module.SmilingKidFalls()
	while true do
		for i = 1, 100 do
			wait()
			workspace.SmilingKidWithRope:SetPrimaryPartCFrame(CFrame.new(workspace.SmilingKidWithRope.PrimaryPart.Position + Vector3.new(0,-1,0)))
		end
		break
	end
end

Is very bad, you can break a for loop without having to use a while loop since it will waste resources.

for i  = 0, 10, 0 do
      print(i)
      break -- will break the first time it prints i
end

Also use workspace instead of game.Workspace since you the script wonā€™t have to index game first.

1 Like

This is a perfect example of the While Wait Do idiom. Your code could be rewritten like

repeat
  wait(interval)
  game.Lighting.ClockTime =  game.Lighting.ClockTime + change
until game.Lighting:GetMinutesAfterMidnight() > 22 * 60

Notice the use of repeat ... until instead of while ... do? This is because we want to check for the condition after the body has ran. Which should produce the exact same result as your original code.

1 Like