I want to create a random food generator for my underwater survival game. I want the food to spawn in 3D, and in a set position. I am finding it really hard to do position the borders of the generator to line it up with the map. Basically, the food sometimes doesn’t spawn, the other time, they do not spawn in the correct place.
Here is the script, it is in ServerScriptStorage:
local P1 = workspace.P1
local P2 = workspace.P2
local P1P = P1.Position
local P2P = P2.Position
local food = game.ServerStorage.Food
for i = 1,15 do
local p = food:Clone()
p.Position = Vector3.new(math.random(P1P.X,P2P.X),math.random(P1P.Z, P2P.Z),math.random(P1P.Y, P2P.Y))
p.Parent = workspace
touching = p:GetTouchingParts()
wait(1)
end
If there is a way to do this using region3s, please tell me.
Vector3s are in XYZ, you gave the math.randoms in XZY, think your
p.Position = Vector3.new(math.random(P1P.X,P2P.X),math.random(P1P.Z, P2P.Z),math.random(P1P.Y, P2P.Y))
Should be
p.Position = Vector3.new(math.random(P1P.X,P2P.X),math.random(P1P.Y, P2P.Y),math.random(P1P.Z, P2P.Z))
I originally did that, but it didn’t work. I might of made a typo, so I’ll try it.
1 Like
It still didn’t work. the output says this:
ServerScriptService.FoodGenerator:9: Invalid argument # 2 to ‘random’ (interval is empty) - server -
here is the updated code:
local P1 = workspace.P1
local P2 = workspace.P2
local P1P = P1.Position
local P2P = P2.Position
local food = game.ServerStorage.Food
for i = 1,15 do
local p = food:Clone()
p.Position = Vector3.new(math.random(P1P.X,P2P.X),math.random(P1P.Y, P2P.Y),math.random(P1P.Z, P2P.Z))
p.Parent = workspace
touching = p:GetTouchingParts()
wait(1)
end
When I originally tested this design, i put -100,100 in the first math.random()
function, and the same in the 2nd one. It spawned them along the X and Z axises (I don’t know the plural for axis).
Hmm, that’s odd, what are the coordinates of both the parts?
P1 is: -440, 499.5, -313.75
P2 is: 6, 41.25, -313.75
They are underwater. The map is big.
I think the issue may be the way it is set up, for the X it’ sfine, for the y, P1’s Y position is greater than P2s, and math.random’s parameters are min and max, and the min is greater than the max. And I think the Z would be fine although not sure if something happens if they’re the same
You may have to tweak the positions around or the code around, you probably have to flip P2P.Y
and P1P.Y
around
That or something is wrong with obtaining the positions, print them out
1 Like
doesn’t seem to work. Originally, P1 & P2 weren’t on the same axis, they were arranged in a way like creating a region3, and that could be causing some issues.
I think I fixed it, the problem was the placement of P1 & P2, the food now spawns inside the area in 3D. Thank you!
1 Like
Glad to be of help to eventually help you reach a conclusion! If you have anymore issues don’t be afraid to make another post!
1 Like