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

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

Random is a value between two values or variables.
math.random(base,base) is like saying math.random(1,1). There’s no range to get a random number from.

1 Like

So is there a “Math.Random” For non numbers? or will I have to convert the position into numbers for the math.random?

You can’t pass a vector3 as a number. You would need to get x,y or z to use the math.random method.

1 Like

Try this for the position line:
Position = startPos + Vector3.new(math.random(base.Position.X - 5, base.Position.X + 5), 0 , math.random(base.Position.Z - 5, base.Position.Z + 5))

This should tween the part to a random area within a 5 stud radius of the base part. Feel free to adjust

1 Like

What does the variable Base equal? This is very important.

It’s important because math.random(base,base) is just calling the value between whatever Base is.

If Base is a variable that is an actual number then math.random(base, base+20) could be used to choose from a range of base and base plus 20. If base equalled 10 then the math.random value range would be from 10 to 30, or math.random(10,30).

1 Like

A part in the workspace.

2 Likes

Then as @CipherFunctions said you can’t just use Base as a value range. You need a range around that Part.
Try their script.

Do you mean that you want to tween it in a limited area? Like it will go a maximum of 10 Y away it’s solved

1 Like

This worked! However It moves to a position studs away from base. However it is a position relative to the base, I just gotta do some bug fixes and this might just work!

thank u

What I meant was, I only want the part to be able to tween randomly INSIDE the parameters of “base” moving only inside the area it covers.

I got it, looks like it’s solved. Have a nice day!

In that case, check if this works.

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(5)
	TweenService:Create(part, TweenInfo.new(waitTime), {
		Position = startPos + Vector3.new(math.random(-(HalfBaseSizeX), HalfBaseSizeX), 0 , math.random(-(HalfBaseSizeZ), HalfBaseSizeZ))
	}):Play()
	task.wait(waitTime)
end
1 Like

This Worked! you mad man you did it! Thank you again for your help with this.

You too thanks very much for your help