Does returning an object value build up memory?

for example

function createpart(name)
local part = Instance.new("part", game.Workspace)
part.Name = name
-- more code etc etc
return part
end

Does part stay there without getting garbage collected or do I have to insert part into a table and then at the very end set part to nil.

It will get garbage collected if you don’t hold a reference to the part. All references to the part within createpart are lost when it finishes but if any variable points to the return value of createpart then that part will survive until that reference is lost.

it’s weird because memory keeps building up in my game and everything gets destroyed after each round ends. References and tables are set to {} too.

it’s hard to understand cause it goes from 330 to 1200 MB server usage and never comes down

function createpart(name)
    local part = Instance.new("Part")
    part.Name = name

    -- more code etc etc

    part.Parent = workspace
    return part
end

you should set the parent last