Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to Position Region3 in front of the player?

Asked by 3 years ago

I just started learning about region 3, and I want to make a region that appears in front of the player when he presses a key. The problem is that the region3 always get in the same direction no matter how I move the player. can someone help me settle it please??

Here is the script,

local c = plr.Character

    local Reg = Region3.new(c.HumanoidRootPart.Position-Vector3.new(0,2.5,2.5),c.HumanoidRootPart.Position  +Vector3.new(20,2.5,2.5))

    local Part = Instance.new("Part",workspace)
    Part.CFrame = Reg.CFrame
    Part.Size = Reg.Size
    Part.Anchored = true
    Part.CanCollide = false
    Part.Transparency = 0.8

    local Rtable = workspace:FindPartsInRegion3(Reg,c)


    for i,v in pairs(Rtable) do
        print(v)
        if v.Parent:findFirstChild('Humanoid') and v.Parent.ClassName == 'Model' and v.Parent:findFirstChild('AoEDebounce')== nil then
            local Deb = Instance.new('BoolValue',v.Parent)
            local BV = Instance.new("BodyVelocity",v)
            BV.MaxForce = Vector3.new(25000,25000,25000)
            BV.Velocity = c.HumanoidRootPart.CFrame.LookVector * 50
            game.Debris:AddItem(BV,0.5)
            Deb.Name = 'AoEDebounce'
            game.Debris:AddItem(Deb,1)
            v.Parent.Humanoid:TakeDamage(30)
        end
    end

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This is because you gave constant vectors (vectors that always stay the same) to specify a direction in your Region3 arguments. You must base it off vectors that will be based off the player's direction, namely the unit vectors which you have used one (CFrame.LookVector).

By using CFrame, it will be as simple as changing your Reg variable to this although I will add a CFrame variable and number variable to make the Reg variable simpler and shorter to read:

local cf = c.HumanoidRootPart.CFrame
local Z_multiplier = c.HumanoidRootPart.Size.Z/2 -- becomes negative later because -Z is forward, by multiplying position Z by this, you will reach the front surface of the object
local Reg = Region3.new(cf * CFrame.new(0,2.5, -(Z_multiplier + 2.5) ), cf * CFrame.new(20,2.5, Z_multiplier + -(Z_multiplier + 2.5) ))

You might need to adjust the CFrame coordinates for X or Y if the result is still undesirable.

0
I tried your idea but it still doesn't work. It say: invalid argument #1 to 'new' (Vector3 expected,got CFrame) SaitamaSaan 9 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

If only it was about CFrame, but, in region3 you can only use Position / Vector3 :( . I tried your idea but it still doesn't work. It say: invalid argument #1 to 'new' (Vector3 expected, got CFrame)

Log in to vote
0
Answered by
A_thruZ 29
3 years ago

Try using @ArtFoundation's answer and use CFrame.Position wherever you are currently using CFrame.

Answer this question