I’m trying to create a random Boulder Generator for my game (or experience roblox likes to call).
In the output it shows invalid argument #1 to 'random' (number expected, got Vector3)
The script (its just one)
local Boulders = game.ReplicatedStorage.Boulders:GetChildren()
function GenerateBoulders(howmany)
if howmany > 0 then
for z = 1, howmany do
local RandomCorner = math.random(Vector3.new(workspace.Corner1.Position),Vector3.new(workspace.Corner2.Position))
local RandomBoulder = Boulders[math.random(#Boulders)]:Clone()
RandomBoulder.Parent = workspace
RandomBoulder.Position = RandomCorner
end
end
end
So what you can do is save the positions in a table.
local PositionsTable = {
["Corner 1"] = {123,24,243}, -- Position after the equals " = "
["Corner 2"] = {213, 12, 465},
-- So on
}
-- Now you can just do
local RandomCorner = math.random(#PositionsTable)
RandomBoulder.Position = CFrame.new(PositionsTable[RandomCorner])
I believe this should work, However this was a bit rushed. Just to let you know you can only randomise integers/numbers, not vectors, Cframes etc.
If the numbers aren’t fixed then after the equals in the table you can just do workspace.Corner1 and then when called add .Position at the end.
math.random requires a Number value, not a Vector3 value
You’ll have to reference it in a different way, by choosing a random number of 1 & 2 instead, then positioning it to that specified place
local Boulders = game.ReplicatedStorage.Boulders:GetChildren()
function GenerateBoulders(howmany)
if howmany > 0 then
for z = 1, howmany do
local RandomCorner
local RandomPos = math.random(1, 2)
if RandomPos == 1 then
RandomCorner = workspace.Corner1.Position
elseif RandomPos == 2 then
RandomCorner = workspace.Corner2.Position
end
local RandomBoulder = Boulders[math.random(#Boulders)]:Clone()
RandomBoulder.Parent = workspace
RandomBoulder.Position = RandomCorner
end
end
end
Unless if you want the the boulders to be positioned in-between those 2 corner positions, then you’d need to “Lerp” them
I see, so you’d need to lerp between the 2 positions then
Try doing this?
local Boulders = game.ReplicatedStorage.Boulders:GetChildren()
local Corner1 = workspace.Corner1
local Corner2 = workspace.Corner2
function GenerateBoulders(howmany)
if howmany > 0 then
local
for z = 1, howmany do
local PositionRandomizer = math.random(1, 100) * 0.01
local RandomBoulder = Boulders[math.random(#Boulders)]:Clone()
RandomBoulder.Parent = workspace
RandomBoulder.Position = Corner1.Position
RandomBoulder.Position:Lerp(Corner2.Position, PositionRandomizer)
end
end
end
This might be due to the fact that there’s literally no cooldown in-between the loop
Not sure if this would fix it
local Boulders = game.ReplicatedStorage.Boulders:GetChildren()
local Corner1 = workspace.Corner1
local Corner2 = workspace.Corner2
local RunService = game:GetService("RunService")
function GenerateBoulders(howmany)
if howmany > 0 then
local
for z = 1, howmany do
local PositionRandomizer = math.random(1, 100) * 0.01
local RandomBoulder = Boulders[math.random(#Boulders)]:Clone()
RandomBoulder.Parent = workspace
RandomBoulder.Position = Corner1.Position
RandomBoulder.Position:Lerp(Corner2.Position, PositionRandomizer)
RunService.Heartbeat:Wait()
end
end
end
Ok so after doing some quick searching I was configuring the loop wrong
I need to actually set its position, then Lerp it next whoops.mov
local Boulders = game.ReplicatedStorage.Boulders:GetChildren()
local Corner1 = workspace.Corner1
local Corner2 = workspace.Corner2
local RunService = game:GetService("RunService")
function GenerateBoulders(howmany)
if howmany > 0 then
for z = 1, howmany do
local PositionRandomizer = math.random(1, 100) * 0.01
local RandomBoulder = Boulders[math.random(#Boulders)]:Clone()
RandomBoulder.Parent = workspace
RandomBoulder.Position = Corner1.Position:Lerp(Corner2.Position, PositionRandomizer)
---RunService.Heartbeat:Wait()
end
end
end
Thing is though, were you wanting to create random positions for the Boulders between the 2 corners? Or just near them? Cause you could add an offset value so that they’re in-between 1 corner & yet still able to roll (Still a bit confused)
function RandomWithinPart(Part)
local randomCFrame = Part.CFrame * CFrame.new(random:NextNumber(-Part.Size.X/2,Part.Size.X/2), random:NextNumber(-Part.Size.Y/2,Part.Size.Y/2), random:NextNumber(-Part.Size.Z/2,Part.Size.Z/2))
return randomCFrame
end
This function will give you a random cframe within a part. I would just make one big, anchored, no collided, non-touchable part that has no physics sim attached and is completely transparent. This will account for rotation/orientation of the part as well. This is the same way ZonePlus does their Zone:getRandomPoint() method. You will need an instance of Random defined as random somewhere in the script.