Every time I try to run this script I get a error saying:
Workspace.Script:16: bad argument #2 to 'random' (number expected, got nil)
Here is the output:
10
10
10
Workspace.Script:16: bad argument #2 to 'random' (number expected, got nil)
Script 'Workspace.Script', Line 16 - global SpawnRandomPostion
Script 'Workspace.Script', Line 26
Stack End
Script:
--[[Purpose of script: This script is suppose to spawn a part in a random spot using the baseplate's position--]] ------------------------Vairbles----------------------- BaseplatePositions= { BaseplateX = game.Workspace.BasePlate.Position.X, BaseplateY= game.Workspace.BasePlate.Position.Y, BaseplateZ = game.Workspace.BasePlate.Position.Z } for i,v in pairs (BaseplatePositions) do--Check print(v) end ------------------------Function------------------------ function SpawnRandomPostion(X,Y,Z) local Cordinates ={ PartX = math.random(0,X), PartY = math.random(0,Y), PartZ = math.random(0,Z) } for k,o in pairs (Cordinates) do--Check print(o) end return Vector3.new(Cordinates[1],Cordinates[2],Cordinates[3]) end --------------------------------------------------------- Part = Instance.new("Part",game.workspace) RPosition = SpawnRandomPostion(BaseplatePositions[1],BaseplatePositions[2],BaseplatePositions[3]) Part.Position = RPosition Part.Anchored = true print(RPosition)--Check
Why did I get this error and how do I fix it?
If you manged to fix it show me how you did it,thank you.
The problem, as it says, is that the "second argument to math.random
is nil
".
That means X
is nil
. X
was the first argument passed to SpawnRandomPosition
, so that must mean that BaseplatePositions[1]
is nil
.
If you check that (print
it out) you will in fact see that it's nil
. Why?
Because the BaseplatePositions
has three things in it, but none of them are at 1
, 2
and 3
-- they are at "BaseplateX"
, "BaseplateY"
, and "BaseplateZ"
.
Thus you could fix this like this:
RPosition = SpawnRandomPostion( BaseplatePositions.BaseplateX, BaseplatePositions.BaseplateY, BaseplatePositions.BaseplateZ)
OR you could fix this by changing the definition of BaseplatePositions
to not have the BaseplateX =
, etc.
You will encounter a very similar problem on line 22.