How would I generate a potato in a random area of a garden?

I’m making a game where you collect potatos and earn money, and then buy food because if your hunger and thirst bars go down, you die. I wanted to make so when you collect a potato, it is destroyed and another generated in a random location in the garden. I’m stuck here:

while wait(0.2) do
	
	for i,v in ipairs(game.Workspace.Potatos:GetDescendants()) do
		
		if v:IsA("ClickDetector") then
			
			v.MouseClick:Connect(function(player)
				local potatos = player.Potatos
				potatos.Value = potatos.Value + 1
				v:Destroy()
				
				local Part = game.Workspace:WaitForChild("Garden"):WaitForChild("Fence"):WaitForChild("Floor")
				
				
				local potatomodel = game.ReplicatedStorage.Potato:Clone()
				
				potatomodel.Parent = game.Workspace.Potatos
				potatomodel.Position = -- Stuck here .-.
				
				
			end)
			
		end
		
	end
	
end

My brain is uncapable of processing very complex math equations, so I really don’t have an idea of what to do. I even tried making something out of this region3 formula

Region3.new(Part.Position-(Part.Size/2),Part.Position+(Part.Size/2))
-- to
local random = math.random(Part.Position-(Part.Size/2),Part.Position+(Part.Size/2))

potatomodel.Position = random

but it didn’t work.

Ok make the part you want the potatoes to teleport to. Then make a vector3.new(copy and paste the position I that part you want the potatoes ot teleport to.)

I actually wanted to make it totally random, without having to create 10000 parts to have a position for each coordinate.

So take the garden bed’s X/Z dimensions(If the garden size is 5, 2, 8, the XZ dimensions are 5 and 8) by doing

local maxX, maxZ = gardenBed.Size.X, gardenBed.Size.Z

You then want to generate a random position in this, using math.random(). So do the following:

local randX = math.random(0, maxX)
local randZ = math.random(0, maxZ)

This will return a random whole number between 0 and MaxRange.

For the final positioning, you will want to set the potato’s position to the X and Z, but relative to the garden bed. So do this:

local potato = potatoReferenceToPart/ModelHere
-- The line below will get the gardenBed's CFrame in the 0, Y, 0 corner, so then you can add the x and z to it. Y is half the size of the gardenBed, so in the middle of the garden bed.
local gardenBedOffsetCF = CFrame.new(Vector3.new(gardenBed.CFrame.X - maxX, gardenBed.CFrame.Y, gardenBed.CFrame.Z - maxZ)

-- We now want to make the potato go to the X/Z relative to the gardenBed's offset CFrame
local relativePos = gardenBedOffsetCF:ToObjectSpace(CFrame.new(0,0,0))
-- Now we have a relative position to the corner of the garden bed.

-- We can now add our coordinates
local potatoPos = Vector3.new(relativePos + CFrame.new(randX, anyYValueThatMakesThePotatoTheRightHeightOutOfTheGround, randZ))

You can now set the potato’s position to the potatoPos, and it will be relative to the garden. If you want to convert it back to world space, you should use :ToWorldSpace(), linked from the API reference here

Of course, you will have to modify it a little bit to fit your needs, but the overall math should stay the same. I hope this helps, please mark as a solution so people can find this in google a few years from now!

1 Like

You’re almost there with the maths. math.random accepts two numbers, a minimum value and a maximum value. You’re attempting to call it with a Vector3.

You just need to separate these into their own variables, then call math.random on the X and Z axes of both resulting Vector3s.

Edit: Like that :point_up:

ninja-cat_1f431-200d-1f464 ninja’d

It somehow works, but it tp’es to the right of the last position, and gives this error: ServerScriptService.Leaderstats :145: invalid argument #2 (Vector3 expected, got CFrame)
Which is this line:

potatomodel.Position = Vector3.new(relativePos + CFrame.new(randX, 1, randZ))

So I changed it to potatomodel.Position = Vector3.new(relativePos + Vector3.new(randX, 1, randZ)), but it gets destroyed after about 2 seconds. Do you know a workaround for this?

You either need to set the CFrame property of the potato when using @Cyafu’s script, or convert the resulting CFrame to a Vector3 by using the Position property of the resulting CFrame.