Spawning coins in certain areas, ignoring interior area

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Basically I want my coins to spawn somewhere else than inside buildings, since they randomly spawn in certain areas of multiple Parts i’ve added!

  1. What is the issue? Include screenshots / videos if possible!

https://gyazo.com/c77d503dc10bd7a05a28441a508964f2

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local function CheckAreaofCoins()
	for i,K in pairs(workspace.Houses:GetChildren()) do
		local CFrameHouseArea = K:GetExtentsSize().X * K:GetExtentsSize().Z
		return CFrameHouseArea/2; -- I included /2 cause I was testing how I can do this..
	end
end

for i,v in pairs(workspace.Coinspawners:GetChildren()) do
						local sa = v.Size.X * v.Size.Z;
						for i = 1, 20 do
							local coin = game:GetService("ServerStorage"):FindFirstChild("Coin"):Clone()
							coin.Parent = workspace.Folder
							coin.CFrame = v.CFrame * CFrame.new(math.random(-v.Size.X / 2, v.Size.X / 2)-math.random(5,10), 1.5, math.random(-v.Size.Z / 2, v.Size.Z / 2)-math.random(5,10))
							if coin.CFrame == CheckAreaofCoins() then
								print("true;")
							end
						end;
					end;

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

So The Printing doesn’t work, And I’m unsure if it’s the Way the function is, or if it’s only getting the Size X and Size Y of the models, I want it to return CFrame of the Size and the Y… How would I be able to get this returned into a CFrame, and then be able to use it so it detects my Coin is inside any of that Area of the house?

1 Like

One way to tell that the code isn’t correct is to see that CheckAreaOfCoins returns a number, while the if statement compares that to a CFrame. A number will NEVER equal a CFrame, so the true branch of the if statement can NEVER run.

There is no built-in way to do collision detection between two boxes, so you’ll have to roll your own like you tried. Fortunately it’s not too hard.

Here’s the code I used to get it to work:

local p = game.Workspace.Part

function isPointInModelBoundingBox(point, model)
	local cf, s = model:GetBoundingBox()
	
	--Get the point, but how it is relative to the center of the model (also takes rotation of the model into account)
	local po = cf:PointToObjectSpace(point) 
	
	return 
		po.X > - s.X / 2 and
		po.X <   s.X / 2 and
		po.Y > - s.Y / 2 and
		po.Y <   s.Y / 2 and
		po.Z > - s.Z / 2 and
		po.Z <   s.Z / 2
end

function isPointInsideHouses(point)
	for _, house in pairs(game.Workspace.Houses:GetChildren()) do
		if isPointInModelBoundingBox(point, house) then
			return true
		end
	end
	
	return false
end

while wait() do
	print(isPointInsideHouses(p.Position))
	if isPointInsideHouses(p.Position) then
		p.BrickColor = BrickColor.Red()
	else
		p.BrickColor = BrickColor.Green()
	end
end

And here’s a test place you can download

ModelCollisionDetection.rbxl (29.9 KB)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.