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

How do I make a brick tween randomly inside a certain area?

Asked by 2 years ago

To do this project I decided to use tween service. The main goal was to make it tween randomly in the confinement of "Base" in workplace. Base Is a small pad on the ground shaped like a brick but on the ground covering an area.

The script:

local TweenService = game:GetService("TweenService")
local base = script.Parent.Parent.Base
local part = script.Parent
local startPos = part.Position

while true do
    local waitTime = math.random(5)
    TweenService:Create(part, TweenInfo.new(waitTime), {
        Position = startPos + Vector3.new(math.random(base,base), 0 , math.random(base,base))
    }):Play()
    task.wait(waitTime)
    end

This may be confusing to read because the script goes between lines on line 9, but Just read it normally and ignore that.

The problem i've been having is the error code " invalid argument #1 to 'random' (number expected, got Instance)" Which is connected to the ;

(math.random(base,base), 0 , math.random(base,base))

Part in this script

any help is appreciated

2 answers

Log in to vote
0
Answered by 2 years ago

I managed to make it work.

Turns out Math.Random Doesnt work with an Instance.

The fix:

local TweenService = game:GetService("TweenService")
local base = script.Parent.Parent.Base
local part = script.Parent
local startPos = part.Position

local HalfBaseSizeX = base.Size.X / 2
local HalfBaseSizeZ = base.Size.Z / 2

while true do
    local waitTime = math.random(1,2)
    TweenService:Create(part, TweenInfo.new(waitTime), {
        Position = startPos + Vector3.new(math.random(-(HalfBaseSizeX), HalfBaseSizeX), 0 , math.random(-(HalfBaseSizeZ), HalfBaseSizeZ))
    }):Play()
    task.wait(waitTime)
end
Ad
Log in to vote
0
Answered by 2 years ago

If you want it to tween to a random position inside the base, just set min argument for X to the left of the base and max to the right, and the same for the Z axis

local TweenService = game:GetService("TweenService")
local base = script.Parent.Parent.Base
local part = script.Parent
local startPos = part.Position 
local waitTime = math.random(5)

while true do
    task.wait(waitTime)
    local leftOfBaseX = (base.Position.X - base.Size.X/2)
    local rightOfBaseX = (base.Position.X + base.Size.X/2)
    local leftOfBaseZ = (base.Position.Z - base.Size.X/2)
    local rightOfBaseZ = (base.Position.Z + base.Size.Z/2)
    TweenService:Create(part, TweenInfo.new(waitTime), {Position = Vector3.new(math.random(leftOfBaseX, rightOfBaseX), base.Position.Y + base.Size.Y/2, math.random(leftOfBaseZ, rightOfBaseZ))}):Play()
    waitTime = math.random(5)
end

Answer this question