Hi Robloxians, I was wondering how can I make a script that spawn parts on a specific part. For example I make a part named ‘floor’ and I want parts spawn randomly or at some specific spawns on that floor part.
Any ideas on how to make the same?
Edit: I forgot to mention that my part that is meant to be spawned and cloned is a model
local yourPart = script.Parent -- its main part
local partToSpawn = script.myPart -- part to clone
local index = 3 -- how many times to repeat
local range = 10 -- how far should it spawn from the part
for i = 1, index do
local clonedPart = partToSpawn:Clone()
clonedPart.Position = yourPart.Position + Vector3.new(math.random(range, -range), math.random(range, -range), math.random(range, -range))
clonedPart.Parent = workspace
end
In the post he said that he would like it to spawn on the floor. Your script is good, it’s just that for the Y position, I would take the Y position of the floor, add half the Y size of the floor, and add a number depending on your liking. This isn’t the best way to do it but if the objects changes position an size sometimes this is the way, else the “add half the Y size of the floor” is unnecessary.
I think the code is fine. The code may be basic but at least it worked ( I tested it ).
local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Stuff") -- Your desired stuff
local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn
while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
for i = 1, HowManyStuffs do
local newStuff = StuffOrSomethingIDK:Clone()
newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), Floor.Size.Y ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor
newStuff.Parent = workspace
task.wait(WaitTime)
end
end
Edit : Added wait time
Another Edit : Change it so the part will spawn on top of the floor
I am using it as a gem which heals the player on touch, the models has 2 mesh one inside and one outside and I have scripted it as if outside mesh is touched it gets destroyed and gives health to player
local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Model").YourDesiredPart -- Change the "Your Desired Part" to your desired "part" I guess
local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn
while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
for i = 1, HowManyStuffs do
local newStuff = StuffOrSomethingIDK:Clone()
newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor
newStuff.Parent = workspace
task.wait(WaitTime)
end
end
local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Model").PrimaryPart -- Your desired model PrimaryPart I guess
local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn
while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
for i = 1, HowManyStuffs do
local newStuff = StuffOrSomethingIDK:Clone()
newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor
newStuff.Parent = workspace
task.wait(WaitTime)
end
end
Hey sorry for late from from my side too, I managed to make this script work! But the only problem is I want it to keep spawning until there are a limited amount of parts, say 5 and when there are 5 parts it won’t spawn more until one part is collected/destroyed, what should I do in that case?
Nope I kept the part inside the script and script is in serverscriptservice
here is the edited script: (i mean not edited but current working script)
l
ocal Floor = workspace:FindFirstChild("CoinSpawnFloor1")
local StuffOrSomethingIDK = script:FindFirstChild("HealthGem").DiamondOuter -- Change the "Your Desired Part" to your desired "part" I guess
local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn
while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
for i = 1, HowManyStuffs do
local newStuff = StuffOrSomethingIDK:Clone()
newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor
newStuff.Parent = workspace
task.wait(WaitTime)
end
end