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

How would I check if the mouse CFrame is inside a region3 CFrame?

Asked by 4 years ago
Edited 4 years ago

Let's say you can only build inside a certain restricted area, how would I check if the mouse is inside that area so that you can place in there? I tried this:

spawn(function()
mouse.Move:Connect(function()
    if mouse.Hit.p < region.CFrame.p and region.Hit.p < mouse.CFrame.p then
        print("yep")
    end
    end)
end)

But errored that I can't compare 2 userdata values.

0
these can never be smaller than each other, one of them has to be bigger and the other one smaller Gameplayer365247v2 1055 — 4y

1 answer

Log in to vote
0
Answered by
ScuffedAI 435 Moderation Voter
4 years ago

Since you can't compare userdata with each other, you'll have to compare each individual axis with each other instead.

like such:

-- This function will take two vector as arguments and
-- compare break it down to axis and then compare
-- each axis value with each other.
function compareVectors(v1,v2)
    local x1,y1,z1 = v1.x, v1.y, v1.z
    local x2,y2,z2 = v2.x, v2.y, v2.z
    return x1 < x2 and y1 < y2 and z1 < z2
end

spawn(function()
    mouse.Move:Connect(function()
        local check1 = compareVectors(mouse.Hit.p,region.CFrame.p)
        local check2 = compareVectors(region.Hit.p,mouse.CFrame.p)
        if check1 and check2 then
            print("yep")
        end
    end)
end)


Ad

Answer this question