Random part spawn in certain location

I need help to make a part to randomly spawn in a random position of a certain area like this
image

So far I have found a lot of information about this but keep ending up with this error
invalid argument #2 to ‘random’ (interval is empty)

	local position1 = Vector3.new(math.random(49,-49), 2, math.random(-240, -374))
		local items = game.ReplicatedStorage.Wheat:GetChildren()
		local randomItem = items[math.random(1, #items)]
		wait(1)
		randomItem.Parent = game.Workspace
		randomItem.Position = position1
		randomItem.Anchored = true

Help would very much be appreciated

I believe the first value in math.random needs to be the minimum, the second is the maximum.

In your code it says:

math.random(49,-49)

Rather, it should be:

math.random(-49,49)

Same thing for the second math.random value.

Here’s the fixed code:

local position1 = Vector3.new(math.random(-49,49), 2, math.random(-374, -240))
		local items = game.ReplicatedStorage.Wheat:GetChildren()
		local randomItem = items[math.random(1, #items)]
		wait(1)
		randomItem.Parent = game.Workspace
		randomItem.Position = position1
		randomItem.Anchored = true
2 Likes

seems to have worked thank you!

1 Like

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