How could you check if theres a brick right next to a part or not? This question was based off of those mining games on roblox where they add the illusion of a endless mining area while all they do is add bricks Is this achieved by raycasting on each side to check if theres a brick. Also how could i check if a player is facinga brick with lookvector?
COMMUNITY QUESTION
How could you check if theres a brick right next to a part or not?
1 answer
You can get an array of parts touching it. Using :GetTouchingParts()
parts = script.Parent:GetTouchingParts()
You can use the # Operator to see how many of something is in a table, so if it's 0, nothing is touching it.
parts = script.Parent:GetTouchingParts()
if #parts >= 0 then
print("No parts touching")
end
Now because it's a table we could do special things with it like use a generic for loop.
parts = script.Parent:GetTouchingParts()
for _,part in pairs(parts) do
print(part.Name.." is touching my parent.")
end
#Final Product
local parts
while wait() do --Not the best way to do this
parts = script.Parent:GetTouchingParts()
end
Hope it helps!
#Other Stuff DragonODeath gave me an idea. I thought about using the Touched event. I thought about it before and realized it isn't very reliable, but I thought of a solution:
parts = {}
function NotOnTable(part)
for _,p in pairs(parts) do
if part ~= p then
return false
end
end
return true
end
script.Parent.Touched:connect(function(p)
if NotOnTable(p) then
local pos = #parts+1
table.insert(parts, p,pos)
local num = Instance.new("IntValue", p)
num.Name = "TableThingForScript"
num.Value = pos
end
end)
script.Parent.TouchEnded:connect(function(p)
for _,pa in pairs(parts) do
if pa == p and p:FindFirstChild("TableThingForScript") then
table.remove(parts,p.TableTingForScript.Value)
p.TableThingForScript:Destroy()
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
WAT. I DID NOT KNOW ABOUT THIS METHOD.