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

Why is this function returning 0,0,0 for a position?

Asked by 5 years ago

I'm trying to make it so it can calculate a position from all 4 top corners and the middle of the top 4 sides of a part.

Every time I run this function it keeps printing out (0, 0, 0) even though the Part isn't positioned at the origin.

local vector3 = Vector3.new
local random = Random.new()

local function returnRandomPos(objectPos, objectSize)
    local posLibrary = {
        vector3(objectPos + vector3(-objectSize.X, 1.5, -objectSize.Z)),
        vector3(objectPos + vector3(objectSize.X, 1.5, objectSize.Z)),
    }

    local index = random:NextInteger(1, #posLibrary)
    return posLibrary[index]
end

local object = workspace.Part

local randomPos = returnRandomPos(object.Position, object.Size)

print(randomPos)

1 answer

Log in to vote
1
Answered by 5 years ago

Hopefully this helps in some way:

local function returnRandomPos(objectPos, objectSize)
    local posLibrary = {
        objectPos * Vector3.new(-objectSize.X, 1.5, -objectSize.Z),
        objectPos * Vector3.new(objectSize.X, 1.5, objectSize.Z),
    }
    return posLibrary[math.random(1, #posLibrary)]
end

local object = workspace.Part

local randomPos = returnRandomPos(object.Position, object.Size)

print(randomPos)
0
It was because you were putting a vector3 inside a vector3 and trying to add them together. I think. JayzYeah32 62 — 5y
0
Yeah I figured that out. Thanks though. YabaDabaD0O 505 — 5y
Ad

Answer this question