You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want my script to make a part green if a part is not inside of it and red if there is a part inside of it
-
What is the issue? Include screenshots / videos if possible!
I don’t know why its not working
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried Devforum and YT.
The reason your code does not work is because it is not constantly being updated. An alternative to GetTouchingParts() is the TouchEnded event.
Try using this code for your script:
local isTouching = false
Structure.Touched:Connect(function(touchPart)
Structure.Color = Color3.fromRGB(255, 0, 0)
isTouching = true
end)
Structure.TouchEnded:Connect(function(touchPart)
Structure.Color = Color3.fromRGB(0, 255, 0)
isTouching = false
end)
it sometimes works, but sometimes it doesnt work, why is that?
I believe this is because the script checks if any part is touching/stopped touching the ‘Structure’ (including a player character).
Are you trying to detect if a specific part is touching the Structure? If so, you will need to have a check under both Touch events to see if the touched part is that specific part you are trying to detect.
Example:
local isTouching = false
Structure.Touched:Connect(function(touchPart)
if touchPart.Name == "" then-- name of specific part here
Structure.Color = Color3.fromRGB(255, 0, 0)
isTouching = true
end
end)
Structure.TouchEnded:Connect(function(touchPart)
if touchPart.Name == "" then-- name of specific part here
Structure.Color = Color3.fromRGB(0, 255, 0)
isTouching = false
end
end)
it still doesn’t work when i add in the name of the part, but also i just want it to detect if its touching any part not a specific one
but i am using a local script, idk if thats why
:GetTouchingParts returns a table with its parts otherway it’s just empty and your for i,v in pairs(Touchingparts)
won’t even iterate
print(workspace.Part:GetTouchingParts())
Output: {}
Checking if the table contains anything
if #TouchingParts == 0 then
-- no touching BasePart
else
-- at least 1 touching BasePart
end
“#
: An unary operator that return the length of the a string or a table”
Note that your code is only running once