I made a coin spawning system, how do i avoid the coins spawning inside of walls and such?
My first idea would be to have the coin itself delete if it detects a part, but I’m not sure if this is the best solution.
I then thought, what if i made zones like this?
These zones would have a script which makes them destroy any coin that touches them.
But in order for me to make that would I’d need the part to detect if it’s touching another part.
How would I do that?
You could generate the position the coin would spawn on, and then check if the coin would be intersecting an object. If it would intersect, then don’t spawn it.
Workspace has a few functions for detecting if a part is intersecting with another. One is called GetPartBoundsInBox() i think. You should read the workspace api page.
How would I go about detecting if the coin is intersecting an object? I can’t find it on the api.
local ReplicatedStorage = game:GetService("ReplicatedStorage") --Rstorage
local workspace = game:GetService("Workspace") --Workspace
local Coin = ReplicatedStorage.BoringCoin --What is the coin?
while true do
local newCoin = Coin:Clone() --local makes a new coin (idk how it works i have to learn later)
newCoin.Parent = game.workspace --what is the parent of the coin? (properties)
newCoin.Position = Vector3.new(math.random(-114.5, 149.9), 4.3, math.random(-154.6, 109.9)) --where does the coin spawn in at (position wise) (properties)
wait(script.Parent.coinspersecond.Value)
end
this is the script
You can use a method (on all BaseParts, won’t work on models don’t think, so just use a part in the model) called GetPartsInPart()
to get a list of all the parts the object is intersecting with
Use this: WorldRoot:GetPartBoundsInBox (roblox.com), just as @HeartBeatStoppah also suggested. You can get the size of the coin by using Model:GetExtentsSize (roblox.com). If the result of :GetPartBoundsInBox()
isn’t empty, that means there is something where the coin would be.
So two alternatives:
A) Use the above resources to decide whether the coin will spawn within something else, and if the result of the above is empty, then spawn the coin. This will prevent the coin from momentarily appearing and then disappearing. I think this approach will look more polished.
B) Spawn the coin, call WorldRoot:GetPartsInPart (roblox.com) as @domboss37 suggested, and if the result is not empty delete the coin. This will cause the coin to appear and then quickly disappear.