Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a Global collisions detector?

Hey guys, so I'm trying to create a destructible game, how-ever I want the world to be able to self-interact, I.E. if you knock a tower down, it falling could crush buildings below. I've already worked out how this could be done, I'd have to just check for collisions on every part in game, and if the colliding block exceeds a velocity, the weld of the collided block is destroyed, or the block itself is destroyed.

The issue that I'm running into is that in order for this to work, I need a global collisions checker, as in, a script that can simultaneously check for collisions on every single part, that way I don't need thousands of scripts running at the same time, which would tank performance. (Also this way makes it so much easier to expand the map.)

Is this even possible, and if so, would a game like this even run without some stupidly intense optimization?

1 answer

✓ Accepted answer

A good option would be to put all the parts you need that require a collision detection inside a folder, than put a script inside the folder with the following.

local Debounce = false --Add debounce to prevent extreme amounts of lag

for index, value in pairs(script.Parent:GetChildren()) do
	if value:IsA("BasePart") then
		value.Touched:Connect(function(hit)
			local human = hit.Parent:FindFirstChild("Humanoid")
			if human and not Debounce then
				Debounce = true
				local Y_velocity = 5
				if value.Velocity.Y >= Y_velocity then
					value:Destroy()
				end
				wait(0.1)
				Debounce = false
			end
		end)
	end
end

Using this, it should take care of at least detecting collision, and since it is all in one script, it shouldn't cause too much lag hopefully.

1 comment

1

Massive thanks! I'll try this out right now

This discussion is preserved as it was. New answers, comments, and votes are closed.