I tried this:
local Part = script.parent
part.Touched:Connect(function(hit)
while true do wait(2)
if hit.parent = nil then
part:Destroy()
This doesn’t seem to work (yes I used end statements just didn’t include it). Any ideas?
I tried this:
local Part = script.parent
part.Touched:Connect(function(hit)
while true do wait(2)
if hit.parent = nil then
part:Destroy()
This doesn’t seem to work (yes I used end statements just didn’t include it). Any ideas?
Hello!
Touched event only fires when a part begins to touch the part. That means "hit" parameter can't be nil and causes your "if hit = nil" statement won't ever be true
I'd recommend you using :GetTouchingParts() to get any parts that are currently touching your part.
Also, :GetTouchingParts() returns nil when a part has no 'TouchInterest', so you'll need to attach TouchInterest.
local part = script.Parent
part.Touched:Connect(function()
-- to add touch interest ( optional ), you can manually do it
end)
while wait() do
local touchingParts = part:GetTouchingParts() -- gets all touching parts
if #touchingParts == 0 then -- if no parts are detected
print("no parts are currently touching")
-- do whatever you want
end
end
Hi, it looks like you have a spelling mistake on the variable Part, line 1, 2, and 5, is it Part or part? Please double check that and make sure its not a spelling mistake and if isn't then maybe this can work:
local Part = script.parent --- Get part location
part.Touched:Connect(function(hit) --- Run function when part is connected
while true do wait(2) ----- While it is true wait 2 seconds
if hit.parent = nil then ------- Instead of this you can try: if Part.Touched == false (Yes two equals) then
part:Destroy() --- If this is true then destroy the part
end
end
The roblox API had some info about that if you wanna double check:
https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched
part.Touched is an event and you can't do if part.Touched == false
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
while wait(2) do instead of while true do wait(2)