Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I detect if the player touches a building?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
--// 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
Ad

Answer this question