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

How do I detect if the player touches a building?

In my game, you touch buildings to get points. I'm only just learning Roblox Lua, so I don't know. My game is set up like this: there is a model called ClonedMap in workspace. Inside of there, there are two folders for Large_Buildings and Small_Buildings respectively. Inside there are models for each of the buildings. How can I use the Touched function to make it detect when the player touches a building, then delete that building?

1 answer

✓ Accepted answer
--// Variables
local ClonedMap = game.Workspace:WaitForChild("ClonedMap")
local Large_Buildings = ClonedMap:WaitForChild("Large_Buildings")
local Small_Buildings = ClonedMap:WaitForChild("Small_Buildings")


for i,v in pairs(Large_Buildings:GetChildren()) do
	for a,k in pairs(v:GetChildren()) do
		if k:IsA("Part") or k:IsA("BasePart") then
			k.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					v:Destroy()
				end
			end)
		end
	end
end

for i,v in pairs(Small_Buildings:GetChildren()) do
	for a,k in pairs(v:GetChildren()) do
		if k:IsA("Part") or k:IsA("BasePart") then
			k.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					v:Destroy()
				end
			end)
		end
	end
end

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