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

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

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

✓ Accepted answer

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)

2 comments

0

It was because you were putting a vector3 inside a vector3 and trying to add them together. I think.

0

Yeah I figured that out. Thanks though.

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