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

How to make tools rain on a certain area?

I got this script from ROBLOX but the problem is it only generates parts and not tools. Is there a way to make a certain area on my game, rain tools, and not parts? Please help, thank you! <3

-- 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 cube =
{
    MODEL = ServerStorage["Cube"],
    DAMAGE = 1,
    NUMBER_PER_MINUTE = 20,
    CLEANUP_DELAY_IN_SECONDS = 5
}
table.insert(fallingStuffTable, cube)

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

-- 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

-- Cleans up the falling object after a delay
local function objectCleanup(object, cleanupDelay)

    delay(cleanupDelay, function()
        if object then
            object:Destroy()
        end
    end)
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)
            setupFallingObjectCollisions(obj, object.DAMAGE)
            objectCleanup(obj, object.CLEANUP_DELAY_IN_SECONDS)
        end
    end

    last = now
end

1 answer

✓ Accepted answer

easiest way given you have no scripting knowledge is where it says

MODEL = ServerStorage["Cube"]

change that to the path to the tool you want to drop, so for example, that table might look like this:

local cube =
{
    MODEL = game.Workspace.Tool, --your tool would have to be in the workspace.
    DAMAGE = 1,
    NUMBER_PER_MINUTE = 20,
    CLEANUP_DELAY_IN_SECONDS = 5
}

also for the function:

local function positionFallingObject(object)

in the first line of the function (line 66), set object = object.Handle because tools aren't BaseParts.

Since this answers your question, then mark it as the accepted answer.

9 comments

0

ouch. what's so wrong bout trying to learn? THANK YOU! <3 ^_^

0

There's nothing wrong with trying to learn, but what you failed to do was the "try" aspect of that. You just went and got a free model and asked us how to fix it for you without attempting it on your own, that is where the problem lies.

0

There are actually people who have posted here, and they said they got it a from a free model, but what they did differently was telling us how they tried, it usually wasn't much and was completely wrong, but they did make that first step that you failed to do.

0

I dont start the part where I should set object = object.Handle. I mean where's the first line of the function? I'm really sorry for being dumb. :(

0

You aren't dumb, you just lack the knowledge. Line 66 is where you set it, and if you do want to actually learn scripting of your own, go to https://developer.roblox.com

0

thank you so much brother! <3

0

uhm, sorry bro but i just tested it and it actually didn't work. :(

0

sorry for the late response, did it show any errors when you ran it?

0

errors will show up in the "output" window

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