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

Loot spawn randomizer problem?

Asked by 2 years ago

So I have my spawn position randomizer figured out, but when the part I want to randomize on is not square it only spawns 2-5 Y studs around the middle of the rectangle but spawns horizontally(90 degrees off) the length of the rectangle

local function SpawnPart(SpawnLocation) local ChosenSpawn = SpawnLocation

    local RNG = Random.new()
    local Part = ChosenSpawn
    local Rx,Ry,Rz = ChosenSpawn.Orientation.X,ChosenSpawn.Orientation.Y,ChosenSpawn.Orientation.Z
    local Position = ChosenSpawn.Position --+ ChosenSpawn.Orientation
    local Size = Part.Size

    local MinX , MaxX = Position.X - Size.X/2, Position.X + Size.X/2
    local MinZ, MaxZ = Position.Z - Size.Z/2, Position.Z + Size.Z/2
    local X, Y, Z = RNG:NextNumber(MinX, MaxX), Position.Y + Item.PickablePart.Size.Y/2  , RNG:NextNumber(MinZ, MaxZ)

    local RanPosition = Vector3.new(X, Y, Z)
    Item:SetPrimaryPartCFrame(CFrame.new(RanPosition))
    -- Get and set the new 'Y' Value by firing a raycast down to find 'Y'
    local Rotation = CFrame.fromEulerAnglesXYZ(0,math.rad(math.random(1,360)),0)
    local rayOrigin = Item.PickablePart.Position
    local rayDirection = Vector3.new(0, -100, 0)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Item}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
    if raycastResult then
        local hitPart = raycastResult.Instance
        if hitPart then
            Item:SetPrimaryPartCFrame(CFrame.new(Vector3.new(X, raycastResult.Position.Y + Item.PickablePart.Size.Y/2, Z))* Rotation)
        end
    end
end

Answer this question