I'm trying to make a certain area in my game to rain random weapons. I found a script from one of ROBLOX's free templates. The script is for falling parts and not tools. I tried to solve it by changing Line 15 to MODEL = ServerStorage["Automatic Rifle"] but it just didn't work and nothing is falling out of the part.

The script I found:

-- Variables for Roblox services
local ServerStorage = game:GetService("ServerStorage")

-- Variable for the drop zone
local dropZone = script.Parent

-- Table that will store info for each falling object
local fallingStuffTable = {}

-- ============================================
-- Falling Objects

local loot =
{
    MODEL = ServerStorage["Wood Crate"],
    NUMBER_PER_MINUTE = 20,
}
table.insert(fallingStuffTable, loot)

-- ============================================

-- Random number generator that will calculate drop chance and random drop position
local randomGenerator = Random.new()

-- Function to check if the given object should drop
local function checkIfShouldDrop(object, delta)

    -- Setup is in number of drops per minute; need to convert minutes to ticks
    local dropsPerSecond = object.NUMBER_PER_MINUTE / 60
    local ticksPerSecond = 1 / delta
    local dropsPerTick = dropsPerSecond / ticksPerSecond

    -- Calculate random chance
    local randomNumber = randomGenerator:NextNumber()
    return randomNumber < dropsPerTick
end

-- Finds a random position inside of the given part and returns a randomly rotated CFrame at that position
local function getRandomCFrameInVolume(partVolume)

    -- Calculate the position component of the random CFrame
    local x = randomGenerator:NextInteger(-partVolume.Size.X/2, partVolume.Size.X/2)
    local y = randomGenerator:NextInteger(-partVolume.Size.Y/2, partVolume.Size.Y/2)
    local z = randomGenerator:NextInteger(-partVolume.Size.Z/2, partVolume.Size.Z/2)
    local localOffset = Vector3.new(x, y, z)
    local worldOffset = partVolume.CFrame:pointToWorldSpace(localOffset)        

    -- Calculate the rotation component of the random CFrame
    local twoPi = math.pi * 2
    local xRotation = randomGenerator:NextNumber() * twoPi
    local yRotation = randomGenerator:NextNumber() * twoPi
    local zRotation = randomGenerator:NextNumber() * twoPi
    local CFrame = CFrame.Angles(xRotation, yRotation, zRotation)   

    -- Add the position and rotation together to get the full random CFrame
    CFrame = CFrame + worldOffset

    return CFrame
end

-- Sets the position of the falling object
-- This function checks if the object is a Part or a Model, as those are positioned differently
local function positionFallingObject(object)

    local objectCFrame = getRandomCFrameInVolume(dropZone)
    if object:IsA("BasePart") then
        object.CFrame = objectCFrame
    elseif object:IsA("Model") then
        if not object.PrimaryPart then
            object.PrimaryPart = object:FindFirstChildWhichIsA("BasePart", true)
        end     
        object.PrimaryPart.CFrame = objectCFrame
    end
    object.Parent = game.Workspace
end

-- Connects the given collision function to the object's "Touched" event, but only if that object is a Part
-- Otherwise, it tries this function on all of the object's children
local function attachCollisionFunction(item, collisionFunction)

    if item:IsA("BasePart") then
        item.Touched:Connect(collisionFunction)
    else
        for _, child in ipairs(item:GetChildren()) do
            attachCollisionFunction(child, collisionFunction)
        end
    end
end

-- Creates the collision function to attach to the object
local function setupFallingObjectCollisions(object, damage)

    -- The collision function; must be declared in this scope as it needs reference to "object" and "damage" params
    local function onTouch(otherObject)
        local character = otherObject.Parent
        local humanoid = character:FindFirstChildWhichIsA("Humanoid")
        if humanoid then
            humanoid:TakeDamage(damage)
            object:Destroy()
        end
    end

    -- Attach the above function to the falling object
    attachCollisionFunction(object, onTouch)
end

-- Update loop; on every loop, checks if an object should be made and, if so, creates that object
local last = tick()
while wait() do
    local now = tick()
    local delta = now - last

    for _, object in ipairs(fallingStuffTable) do
        if checkIfShouldDrop(object, delta) then
            local obj = object.MODEL:Clone()
            positionFallingObject(obj)
        end
    end

    last = now
end

1 comment

0

try using waitforchild?