Possible fix to the Instance.new issue

  • local Temp = {}
    local tempNew = Instance.new
    Temp.new = function(className, parent)
    	local object = tempNew(className)
    	object.Parent = parent
    	return object
    Instance = Temp
    Temp, TempNew = nil

    Would this be a plausible solution to the problem with passing a parent argument into the Instance.new function? I haven't conducted any benchmarking, though in theory, it should work more efficiently.

  • @LateralLace What? That sounds looks like the same thing but with extra steps.

  • For some reason, passing the parent argument into an Instance.new() function causes more lag than defining/setting it afterwards. To work around this, I redefined the function by creating the instance and then setting it's parent.

  • @LateralLace
    Isn't this the same as

    local myobj = Instance.new("Part")
    myobj.Properties = Wutever
    myobj.Parent = game.Workspace


    BTW, you should modify the properties before setting the parent, setting the parent before the properties is also unefficient, or at least that is what I think I understood from the post that informed devs of that "The parent argument of Instance.new() is bad!".

    Anyway, I don't think that using the Parent argument of Instance.new() is really a big issue. Like what may it cause?

    -0.000000000000000001 Frames Per Second?

    I think this has been over-exaggerated. I haven't tried by myself testing if it does really make the frames low down, but seriously just for indicating a Parent the game goes slower?

    Well, I think I talked a lot here. And sorry for not putting the code in a code block, too lazy :v

  • @Aimarekin It may push a thing you don't want, or (with Roblox's physics,) it can make something fly off the map.

  • @LateralLace Assuming you are not resizing it smaller later or repositioning or anchoring, I think this would be good, but I don't think it's really hard to just parent last. Also, TempNew already doesn't exist, why are you setting it to nil?

  • @LateralLace The parent argument was removed as it was being used incorrectly. Often people would set the parent first which put it into the game then setup the instance.


    local part = Instance.new('Part', workspace)
    part.Name = 'a'
    part.CFrame = CFrame.new()

    This would result in the following

    • create part
    • parent to game
    • game connects event
    • put part on list to replicate to players
    • put change name on list to replicate to players
    • put CFrame change on list to replicate to player

    This costs a lot of overhead since we knew how the part should be setup before parenting it to the game.

    We should setup a instance then parent it to the game

    local part = Instance.new('Part')
    part.Name = 'a'
    part.CFrame = CFrame.new()
    part.Parent = workspace

    This would result in the following

    • create part
    • set name
    • set CFrame
    • game connects event
    • put part on list to replicate to players

    This is less to replicate making for better performace.

  • Global Moderator

    @kingdom5 It's not only better for latency, it's also faster for the machine running the code.

  • Yeah I think I got the point awhile ago :3

Log in to reply

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.