Getting error spawing parts random area

Not really sure why a vector3 on a single X, Y or Z axis is necessary. What am I missing?

---- Drop parts onto basePart ----

local basePart = game.Workspace.BasePart
local baseValueX = basePart.Size.X -- get baseParts X size
local baseValueZ = basePart.Size.Z
local baseAreaX = basePart.CFrame + baseValueX / 2 -- find CFrame and then add half the baseParts Size
local baseAreaZ = basePart.CFrame + baseValueZ / 2
local baseAreaNegX = basePart.CFrame - baseValueX / 2
local baseAreaNegZ = basePart.CFrame - baseValueZ / 2
local PositionY = 2

local function makePart()
	local newPart = Instance.new("Part")
	newPart.Size = Vector3.new(1,1,1)
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Name = "SpawnedPart"
	newPart.Parent = game.Workspace
	newPart.Shape = Enum.PartType.Ball
	newPart.BrickColor = BrickColor.new("Lime green")
	newPart.Position.X = Random.new(baseAreaNegX, baseAreaX)
	newPart.Position.Z = Random.new(baseAreaNegZ, baseAreaZ)
	newPart.Position.Y = PositionY
end

makePart()

CFrame should’ve expected at least either a CFrame or Vector3 datatype, but instead you’re trying to add it up with a number datatype instead

Try replacing those lines with this instead:

local baseAreaX = basePart.CFrame.X + baseValueX / 2 -- find CFrame and then add half the baseParts Size
local baseAreaZ = basePart.CFrame.Z + baseValueZ / 2
local baseAreaNegX = basePart.CFrame.X - baseValueX / 2
local baseAreaNegZ = basePart.CFrame.Z - baseValueZ / 2

I ended up with…

You can’t individually set a specific Axis for each .Position, you have to manually create a Vector3 if you want to establish something like that

Replace

	newPart.Position.X = Random.new(baseAreaNegX, baseAreaX)
	newPart.Position.Z = Random.new(baseAreaNegZ, baseAreaZ)
	newPart.Position.Y = PositionY

With this instead:

newPart.Position = Vector3.new(Random.new(baseAreaNegX, baseAreaX), PositionY, Random.new(baseAreaNegZ, baseAreaZ))

Random is not working in your script. First, create a random object called rand. Make it Random.new(). Then, use rand.NextNumber.

I had an feeling that was the case but I couldn’t seem to get it right. Right now it spawns at (0,2,0)

image

---- Drop parts onto basePart ----
local rng = Random.new(time())

local basePart = game.Workspace.BasePart
local baseValueX = basePart.Size.X -- get baseParts X size
local baseValueZ = basePart.Size.Z
local baseAreaX = basePart.Position.X + baseValueX / 2 -- find CFrame and then add half the baseParts Size
local baseAreaZ = basePart.Position.Z + baseValueZ / 2
local baseAreaNegX = basePart.Position.X - baseValueX / 2
local baseAreaNegZ = basePart.Position.X - baseValueZ / 2
local positionY = 2

local function makePart()
	local newPart = Instance.new("Part")
	newPart.Size = Vector3.new(1, 1, 1)
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Name = "SpawnedPart"
	newPart.Shape = Enum.PartType.Ball
	newPart.BrickColor = BrickColor.new("Lime green")
	newPart.Position.X = rng:NextInteger(baseAreaNegX, baseAreaX)
	newPart.Position.Z = rng:NextInteger(baseAreaNegZ, baseAreaZ)
	newPart.Position.Y = positionY
    newPart.Parent = game.Workspace
end

makePart()

Random.new() accepts one parameter, which is the seed: number. The function you needed to use was Random.new(seed: number)s :NextInteger if you wanted an integer (that is, a non-decimal value) or :NextNumber if you wanted a float (that is, a decimal value).

Apparently I didn’t know this, but from what @SubtotalAnt8185 stated before Random() does not work the way you originally thought it did

The way to use Random.new() (After just learning about it 10 seconds after whoops) is creating it into a variable on its own, then using one of the methods provided by it (rand.NextNumber, rand:NextNumber) to get the desired result that you want

  • (If you use rand.NextNumber) Reference the Random() datatype
  • Provide the Minimum Number
  • Provide the Maximum Number

Although I don’t think either the 2 of them matter apart from the parameters, you can call it like this:

---- Drop parts onto basePart ----

local basePart = game.Workspace.BasePart
local baseValueX = basePart.Size.X -- get baseParts X size
local baseValueZ = basePart.Size.Z
local baseAreaX = basePart.CFrame.X + baseValueX / 2 -- find CFrame and then add half the baseParts Size
local baseAreaZ = basePart.CFrame.Z + baseValueZ / 2
local baseAreaNegX = basePart.CFrame.X - baseValueX / 2
local baseAreaNegZ = basePart.CFrame.Z - baseValueZ / 2
local PositionY = 2
local rand = Random.new()

local function makePart()
	local newPart = Instance.new("Part")
	newPart.Size = Vector3.new(1,1,1)
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Name = "SpawnedPart"
	newPart.Parent = game.Workspace
	newPart.Shape = Enum.PartType.Ball
	newPart.BrickColor = BrickColor.new("Lime green")
	newPart.Position.X = rand:NextNumber(baseAreaNegX, baseAreaX)
	newPart.Position.Z = rand:NextNumber(baseAreaNegZ, baseAreaZ)
	newPart.Position.Y = PositionY
end

makePart()

rand:NextNumber works perfectly when combined with JackScarlett’s newPart.Position solution.

Ty both for your efforts.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.