How I could make a item spawner without setting the positions?

How I could make a item spawner without setting the positions for items to spawn? I was thinking in make a raycast system to it, but I dont know if there better methods. I want do it because my game have a big “map”.

1 Like

What did you mean by not setting a position?

Not doing defined positions, like I dont have a certain position for items.

Oh so you mean you want an Item spawner that bring a tool toward it without setting the tool’s position itself toward the spawner?

Yes, because as my map of my game is big, I dont think setting the tool’s position itself toward the spawner is a good idea.

You can try to script a basic tool spawner

Please indicate what you want to do.
Do you want to spawn items relatively to the player or randomly spawn items across your map?

For the first option, you can just position the item relative to the Character’s HumanoidRootPart’s CFrame like this:

local HumanoidRootPart = Character.HumanoidRootPart
local PartToPosition = Instance.new("Part")
PartToPosition.Parent = workspace
PartToPosition.CFrame = HumanoidRootPart.CFrame + (HumanoidRootPart.CFrame.lookVector()*3)

If you want to set it randomly across a map, you can raycast from a grid to locate the ground and place it somewhere random like this:

local Part = Instance.new("Part")
Part.Parent = workspace
local RandomPosition = Vector3.new(math.random(-100,100),1000,math.random(-100,100))
local Raytoground = ray.new(RandomPosition,Vector3.new(0,-10000,0))

local PartOnRay,PositionOnRay= workspace:FindPartOnRay(Raytoground) 

if PartOnRay and PositionOnRay then
 Part.CFrame = CFrame.new(PositionOnRay) 
end

Of course this is just an exemple of what you CAN do to achieve this. If you have anymore questions, just reply!

4 Likes

I don’t understand what you’re asking for, but I will assume that you want to place it at randomly generated positions:

You can use math.random or Random.new to generate a random vector, then you can place the object there. You wouldn’t need raycasting, unless your map isn’t flat. If it isn’t, then you can generate a random vector and position it at a high y-coordinate, then cast a ray downwards and wherever it hits, you’ll set it to that position

Thats what I wanted to do! The second option I will try that thanks.

1 Like

If it works, please flag this as solved!

1 Like

It kinda works, but I got 1 question:
How I could do it in all my map instead of focusing in a single area?

tweak the math.random(min,max) amounts to fit your map =)

Alright soo I can make a part, set it to the size of my map, and put that size of the part in the
random position thing? Also how I can make the parts stop falling from sky?

Try anchoring them, I guess?
Other than that I don’t know.

No, like the parts are spawning at the sky

There’s definitely a part blocking the ground then, try changing this:

local RandomPosition = Vector3.new(math.random(-100,100),1000,math.random(-100,100))

to this

local RandomPosition = Vector3.new(math.random(-100,100),50,math.random(-100,100))

Again, if this is the solution, please flag the post appropriately.

That may be occuring because I made a region system, where your at a certain region it play a sound etc… May it can help with the item spawn?

in that case use FindPartOnRayWithIgnoreList(RayToGround,{Linkyourregionfolderhere!})

Its working now lol, thanks dude.

1 Like

Sorry for late reply but, I still dont understand how to make it spawn in my whole map