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 ;
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.
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
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).
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!
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