BasePart:GetRootPart() returns Instance type, not BasePart

The issue:
BasePart:GetRootPart() returns the incorrect type, aka Instance, not basepart, it won’t autofill basepart’s properties and can cause a bit of confusion.

In this image, it won’t autofill Position property because it’s not basepart type.
image

Although, this function is supposed to be replaced by AssemblyRootPart property, but it’s still supported and this issue should be fixed.

Expected behavior

The actual behavior:
BasePart:GetRootPart() should return BasePart type

2 Likes

HI! Thanks for the report. I think GetRootPart is just very old (maybe even older than typing), hence why it returns an Instance. I will ask around about changing that. In the meantime, like you mentioned, we recommend the use of AssemblyRootPart, which does return a BasePart.

Yeah, started to use it instead, before i used it as a preference :sweat_smile:

Well, i decided to google out about :GetRootPart and it’s actually was supposed to be marked as deprecated in 2020.

source

I also did a benchmarking and it turned out that it’s faster to use already referenced instance property than function that need to execute and return result
Code:

local Result2 do
	local Part = workspace.Part
	local Time = os.clock()
	for _=1, 1000000 do
		local Root = Part:GetRootPart()
	end
	Result2 = os.clock() - Time
end
local Result1 do
	local Part = workspace.Part
	local Time = os.clock()
	for _=1, 1000000 do
		local Root = Part.AssemblyRootPart
	end
	Result1 = os.clock() - Time
end task.wait(1)
print(Result1)
print(Result2)
print(`Part.AssemblyRootPart is faster{100 -(Result1/Result2*100)}% than :GetRootPart()`)

(Need to run a few times for better result)

So yeah, better to mark :GetRootPart as deprecated. While then, i’ll mark solution for now because there’s nothing really to discuss

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.