My Region3.new() is not produce region

So, I made a simulation of what the problem is here:

------------- Module&Service --------------

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer

--------- TweenInfo ---------

local tweenInfo = TweenInfo.new(
	10,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local tweenGoal = {
	Size = Vector3.new(94.45, 109.05, 109.05);
}

local debounce = false

--------- Function ---------

local function ShootFireBlock()
	if debounce then return end
	
	-- get player important instance
	local char = player.Character
	if char == nil then return warn("Character not spawn yet") end
	local humanoid = char:WaitForChild('Humanoid')
	local hmr = char:WaitForChild("HumanoidRootPart")
	
	debounce = true
	
	-- Set so player cant move
	humanoid.WalkSpeed = 0
	humanoid.JumpHeight = 0
	hmr.Anchored = true
	
	-- get the object to workspace
	local object = Instance.new("Part")
	object.Name = "object"
	object.Anchored = true
	object.CanCollide = false
	object.Transparency = 0.5
	object.Color = Color3.fromRGB(213,115,61)
	object.Size = Vector3.new(1.889, 2.181, 2.181)

	-- Create tween instance
	local tween = TweenService:Create(object, tweenInfo, tweenGoal)

	-- Move object to real place
	local lookAt = (hmr.CFrame * CFrame.Angles(math.rad(180), 0, 0)).LookVector
	local originCFrame = hmr.CFrame * CFrame.Angles(math.rad(180), 0, 0)
	object.CFrame = originCFrame
	object.Parent = workspace

	--Play a tween
	tween:Play()

	-- Connect the object size change function
	local connection
	connection = object:GetPropertyChangedSignal("Size"):Connect(function()
		local x = object.Size.X

		local newPos = originCFrame:ToWorldSpace(CFrame.new(0,0,x))
		object.CFrame = newPos

		if x == 94.45 then
			connection:Disconnect()
		end
	end)

	-- region simulate start here
	coroutine.wrap(function()
		
		-- Every 1 second the simulation is showing
		-- The corner block simulation also grown bigger every second
		local times = 0
		while wait(1) and times < 10 do
			times += 1

			local startVector = originCFrame:ToWorldSpace(CFrame.new(object.Size.X/2, object.Size.Y/2, 0)).Position
			local endVector = originCFrame:ToWorldSpace(CFrame.new(-object.Size.X/2, -object.Size.Y/2, object.Size.Z)).Position
			local region = Region3.new(startVector, endVector)
			
			-- This block will continue to the bottom right of the player character 
			local partStart = Instance.new("Part")
			partStart.Name = "Start" .. tostring(times)
			partStart.Anchored = true
			partStart.Position = startVector
			partStart.Size = Vector3.new(times, times, times)
			partStart.Parent = workspace
			
			-- This block will continue to the top left of the player character (and also going forward)
			local partEnd = Instance.new("Part")
			partEnd.Name = "End" .. tostring(times)
			partEnd.Anchored = true
			partEnd.Position = endVector
			partEnd.Size = Vector3.new(times, times, times)
			partEnd.Parent = workspace

			-- Simulate the region
			local part = Instance.new("Part")
			part.Anchored = true
			part.CanCollide = false
			part.Transparency = 0.75
			part.CFrame = region.CFrame
			part.Size = region.Size
			part.Parent = workspace
		end
	end)()

	-- When tween completed, destroy all tween
	tween.Completed:Wait()
	tween:Destroy()

	-- Delete main object
	object:Destroy()
	
	-- Set so player can move again
	humanoid.WalkSpeed = 16
	humanoid.JumpHeight = 7
	hmr.Anchored = false
	
	debounce = false
end

--------- Connection ---------

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.J then
			ShootFireBlock()
		end
	end
end)

You can try putting this on the local script and pressing J button.

The fireball (block) with orange color will be coming out and it will get bigger (using tween) and the position changes every time the size changes so it can move forward in front of the player

(it might look over complicated but this is only a simulation of the real problem that I’ve faced in my game, that is why some of the code might look nonsense)

The big grey block with a bit of transparency represents the region simulation (which in my game will use as a hitbox). IT SHOULD GROW BIGGER FOLLOWING THE FIREBALL OBJECT. But, in my case, it’s not.

The small blocks represent the corner of the region, where the start part will continue on the bottom right corner of the player character, and the end part will continue on the top left corner and forward of the player character.

The very entertaining about this challenge is that: it already works as I intended it to be, but only when the HumanoidRootPart of the player is set at Orientation 0,180,0. So, I was able to get an example of what I want to achieve for you guys to see.

This is what normally happens if HumanoidRootPart did not set at Orientation 0,180,0.

As you guys can see. The difference between these two videos is that the first one produces the region according to the orange fireball object correctly, whereas the second one does not produce any region!? This is kinda weird because if you look at my second video closely, the small blocks (which represent the start and end position of the region) are produced correctly, but the region is not coming out (or sometimes produces a weird shape).

So, hope you guys got what I want to achieve now. If you would like to see it yourself please open the Roblox studio, copy and paste the above script on the local script in starterPlayerScript and you’ll see what I meant.

Ps.
I’m currently developing a game right now, it’s easy-to-play fighting game and have level-up system. If you guys are interested in being a tester or any contributor, please see my game page: https://www.roblox.com/games/7139830370/Internet-PVP. After that join my group and contact me via social media site. I planned to release a very pre-alpha of it at the end of February 2022 for tester only.

Change this line:

local startVector = originCFrame:ToWorldSpace(CFrame.new(object.Size.X/2, object.Size.Y/2, 0)).Position
local endVector = originCFrame:ToWorldSpace(CFrame.new(-object.Size.X/2, -object.Size.Y/2, object.Size.Z)).Position
local region = Region3.new(startVector, endVector)

To:

local startVector = object.Position - object.Size/2
local endVector =  object.Position + object.Size/2
local region = Region3.new(startVector, endVector)

And your result will be what you want:

1 Like

Thanks for the reply!

I’ve expected this solution to be coming. And it really did work with my simulation! However, it does not work on a real problem that I’ve encountered.

This is because the said “object” is not a part, but it’s a model. And I can’t get the size of it. Sorry, that I should make it clear at the beginning, hope you understand.

So, now what happens if the “object” is a model (which has a really complicated task going on inside its child that you can’t reference any value to its child). Is there still a possible way to get it working?

model:GetBoundingBox()
https://developer.roblox.com/en-us/api-reference/function/Model/GetBoundingBox

1 Like

Wow, never know such a function exist.

I’ve tested it and it works great. However, the problem now is moving to the position of the model. I’ve known only GetPivot() and it didn’t work on my case.

My model consists of three main components. The base will always stand there with the player character that constantly rotates around, and the 2 objects will continually scale and move forward. This will shape the model pivot position to always be the same, where the size is continually changed, and that results in region outcome that I don’t want.

The image below explain it all:

As you can see, what I want to achieve is to get the region that covered all the area from base to objects. But sadly, this is what happening to me:

It seems like the model takes the first part that they spawn in as its pivot location. Even the size of the ball is continually changing and moving forward, the pivot model didn’t move a single inch and that’s causing me a headache.

This is the script that I’ve changed to:

local modelPos = model:GetPivot().Position
local modelOrientation, modelSize = model:GetBoundingBox()
local startVector = modelPos - modelSize/2
local endVector =  modelPos + modelSize/2
local region = Region3.new(startVector, endVector)

Is there any way to get the center position of the model? If there is, that would solve the case. Or otherwise use my old code from above and figuring out why the region is not producing?

Ok, so until now I’m not able to find a solution on why my region is not producing.

So, I’ve changed the hitbox to using magnitude instead. Idk why I used region in the first place.

I’ll put this script from @SelfDeclared as a solution instead, cuz most of the time, if you do not use the object size as a region, it causes the problem.

1 Like