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

How do I *properly* remove parts?

I followed the tutorial by roblox on how to make a round based game, but only the randomly falling objects part and modified it a bit

(the tutorial is https://www.youtube.com/watch?v=rsTN0DppMaA&list=PLuEQ5BB-Z1PJ4JIDXpttxuvCzJMeIqzMi&index=17)

However when I leave the game open on test for a while I notice lag build up and Roblox studio freezes proportionately to how long i leave it open when i try to end the test. My assumption then is, despite these parts falling out of the world IE Despawning, as well as having a :Destroy() 1.5 seconds after they come to life on them, Roblox is holding onto some information about them causing something I assume is similar to a memory leak.

Heres my code for spawning and randomly dispersing the rain:

local rainDrop = game.ServerStorage.RainDrop local function createRainDropCopy() local rainDropCopy = rainDrop:Clone() rainDropCopy.Parent = game.Workspace local xPosition = math.random(-50, 50) local zPosition = math.random(-50, 50) local yPosition = math.random(90, 150) rainDropCopy.Position = Vector3.new(xPosition,yPosition,zPosition) end

while true do createRainDropCopy() wait(0.1)

end

And heres my code for despawning them:

wait(1.5) local rainDrop = script.Parent rainDrop:Destroy()

TLDR: When I use :Destroy() to delete my raindrops so they dont stick around, roblox still seems to hold onto some information about them causing the game to get laggier and laggier the longer its open because more of them keep building up.

Curious if anyone has any ideas on how to solve this, and yes im aware this is an inefficient way of doing this so I am also open to suggestions of alternative methods.

Regardless thank you for reading! :)

2 comments

0

Figured I would restate here: Im aware this is not efficient, I am open to alternatives as well as solutions!

0

perhaps the problem is that the loops are stacking? im not too sure try doing script:Destroy() at the end too

1 answer

here the developer documentation says that the destroy function will completely disconnect a part from the game and even mentions that this function is used to prevent memory leaks. I supposed it may be possible that this may be the cause of the lag since it says that object can still be stored in the variable but cannot become the parent of anything else other than nil. Have you looked into other things that may be causing the lag?

1 comment

0

I was just playing around with this so there is nothing else in the game other than a simple day night cycle, i will isolate it and make sure the problem isnt it.

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