I'm trying to make a shop pop up when the player touches the shop. What the code is doing is that it goes through every single body part, checks if it has the function :GetTouchingParts(), inserting it into a table, checking if the table contains any object with the name of "ShopHitbox" and setting plrInteractedWithHitbox to true. Why does this not work?
Code:
01 | local char = script.Parent |
02 | local plr = game.Players:GetPlayerFromCharacter(char) |
03 | local plrGui = plr.PlayerGui |
04 | local plrInteractedWithHitbox = false |
05 |
06 | while not plrInteractedWithHitbox do |
07 | local touchingParts = { } |
08 |
09 | for i, v in pairs (char:GetChildren()) do |
10 | if pcall ( function () local t = v:GetTouchingParts() end ) then |
11 | table.insert(touchingParts, i + 1 , v:GetTouchingParts()) |
12 | end |
13 | end |
14 |
15 | for i, v in pairs (touchingParts) do |
BasePart:GetTouchingParts() returns a table of objects, not the objects themselves.
To fix this, use another for loop. (replace lines 15-19)
01 | for i, v in pairs (touchingParts) do |
02 | for _, p in pairs (v) do |
03 | if p.Name = = 'ShopHitbox' then |
04 | plrInteractedWithHitbox = true |
05 | break -- Break the loop when ShopHitbox is found. |
06 | end |
07 | end |
08 | if plrInteractedWithHitbox then |
09 | break -- When plrInteractedWithHitbox is true, break this loop too. |
10 | end |
11 | end |
Edit 1: Added another break for the touchingParts loop.
Edit 2: Full script with fix as requested (used mobile to add edit after waking up):
01 | local char = script.Parent |
02 | local plr = game.Players:GetPlayerFromCharacter(char) |
03 | local plrGui = plr.PlayerGui |
04 | local plrInteractedWithHitbox = false |
05 |
06 | while not plrInteractedWithHitbox do |
07 | local touchingParts = { } |
08 |
09 | for i, v in pairs (char:GetChildren()) do |
10 | if pcall ( function () local t = v:GetTouchingParts() end ) then |
11 | table.insert(touchingParts, i + 1 , v:GetTouchingParts()) |
12 | end |
13 | end |
14 |
15 | for i, v in pairs (touchingParts) do |