Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you prevent :Connect() functions from causing data leaks?

So I'm making this obby game with different worlds and stuff. It's pretty small, and each world only has around 400 parts. It works perfectly in roblox studio, but when I play it in roblox player, the game becomes unplayable.

I've also noticed that whenever I try something to reduce lag, it works for a few hours but then comes back. The microprofiler isn't showing anything either. Here's a list of things I've tried:

  • Reduced the amount of moving parts at any given moment to less than 10
  • Searched the code for any data leaks
  • Cut down a lot of unions

I'm suspecting that it's a data leak from my kill scripts:

obstacle = script.Parent
Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PartKillModule = require(ReplicatedStorage:WaitForChild("PartKill"))

connection = Players.PlayerAdded:Connect(function(plaer)
	player = plaer
	plr = game.Workspace:WaitForChild(tostring(player))
end)

connection1 = obstacle.Touched:Connect(function(touched)
	PartKillModule.Check(touched)
end)

if not plr == nil then
	connection2 = plr.HumanoidRootPart.Touched:Connect(function(touched)
		if tostring(touched) == "(Insert next checkpoint name here)" then
			connection1:Disconnect()
			connection:Disconnect()
			obstacle = nil
			player = nil
			plr = nil
			Players = nil
			connection2:Disconnect()
		end
	end)
end

Here's the PartKillModule thing:

local PartKill = {}

PartKill.Check = function(touched)
	local all = touched.Parent:GetChildren()
	for i=1, #all, 1 do
		if tostring(all[i]) == "Humanoid" then
			all[i].Health = 0
		end
	end
	all = nil
	return
end

return PartKill

My original kill script never had anything about disconnecting the .Touched:Connect() function and didn't include a module either. I tried disconnecting the function right after it kills the player (if it's touched). It works, but that disables the script right after.

EDIT: I don't think my current script is actually disconnecting though, because it's not solving anything.

I know this post has been jumping around a lot and the question isn't clear, but I'm basically asking how to prevent a data leak but still making the killscript work at least until the player reaches the next checkpoint.

EDIT: Sorry about the confusing variable names, I didn't know any better way to do it.

4 comments

0

Maybe the touched event is firing multiple times. Try added a debounce. Ive never toyed with modules or any of that, but id put the db inside it

0

adding*--- Wouldn't it be crazy though if that simple thing is the solution

0

on wait lol i get what u mean.

0

Yanno? A debounce to prevent the event from firing excessively

1 answer

✓ Accepted answer

Memory leaks aren't just things using a ton of memory. A memory leak occurs when things are using memory that you don't want them to, like a strong reference that you aren't using anymore, however for some godforbidden reason you can't use said reference.

If you truly believe .Touched is the issue, then try disconnecting all functions.

Plus, lag is also not a memory leak, I'd say. Honestly, if you have a crap ton of kill bricks, maybe you should decide if you should use .Touched on the player or if you should use .Touched on the part.

Cheers.

3 comments

0

Sir what is a memory leak?

0

Jesus, I didn't even think of using .Touched on player. Well thought of

0

tbh idk what's actually causing it. I disconnected all the functions before and it worked so I think it's this script, but I honestly have no idea. I'll probably try putting the .touched on the player and ignore this problem for now. If it really is the kill script it'll solve it anyways bc putting .touched on the player means deleting all the repetitive kill scripts i have

This discussion is preserved as it was. New answers, comments, and votes are closed.