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

How would I get the position at the exact center of a model?

Not really much else to say. I've tried the following method-

--returns a list of positions
function PopulateList(model)

	local tbl = {
		x = {},

		y = {},

		z = {},

	}
	
	for _,v in pairs(model:GetChildren()) do
		tbl.x[#tbl.x + 1] = v.Position.X + (v.Size.X / 2)
		tbl.y[#tbl.y + 1] = v.Position.y + (v.Size.y / 2)
		tbl.z[#tbl.z + 1] = v.Position.z + (v.Size.z / 2)

	end
	
	return tbl
	
end

--returns the corners of the object in question
function SortGL(tbl)

	local gx, gy, gz, lx, ly, lz = 0, 0, 0, 1000, 1000, 1000
	local index = 1
	
	for _,v in pairs(tbl) do
		for _,pos in pairs(v) do
			if index == 1 then
				if pos > gx then
					gx = pos
				elseif pos < lx then
					lx = pos
				end
			elseif index == 2 then
				if pos > gy then
					gy = pos
				elseif pos < ly then
					ly = pos
				end
			elseif index == 3 then
				if pos > gz then
					gz = pos
				elseif pos < lz then
					lz = pos
				end
			end
		end
		index = index + 1
	end
	
	gpos = Vector3.new(gx, gy, gz)
	lpos = Vector3.new(lx, ly, lz)
	
	return gpos, lpos
	
end

function GenerateHitbox(obj)

	local gpos, lpos = SortGL(PopulateList(obj))
	
	local hitbox = Instance.new("Part", obj)
	hitbox.Name = "Hitbox"
	hitbox.Transparency = 0
	hitbox.Position = gpos
	
end

... and the result isn't anything like what I wanted (the position is very far off). How would I get the exact center of a model?

2 comments

1

One thing I use to get a Model's CFrame is 'Model:GetModelCFrame()', it gets the CFrame of a Model [While also containing parts].

0

It worked! Thank you. I didn't see this when I looked on the wiki page, for some reason. Still, I'm wondering what exactly IS wrong with my script up top. Hm.

1 answer

Simply use model:GetModelCFrame().p to get the position matrix of the CFrame, but if you want the rotation matrix included, just use model:GetModelCFrame()

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