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

Is there a way to check if a part is no longer touching a part?

Asked by
gitrog 326 Moderation Voter
7 years ago

So, I have this "ShopGUI" script, when someone goes into the store they hit an invisible brick, and a GUI pops up. However, when they leave the store (no longer touching part), I want the GUI to disappear. How would I do this?

Here's my script

script.Parent.Touched:connect(function(touchy)
    local player = game.Players:GetPlayerFromCharacter(touchy.Parent)

    if player then
        if player.PlayerGui:FindFirstChild('StoreGUI') == nil then
            local shopGUI = game.ServerStorage.StoreGUI:Clone()
            shopGUI.Parent = player.PlayerGui
        end
    end
end)

0
use the TouchEnded function.... greatneil80 2647 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago

Use the TouchEnded function.

script.Parent.TouchEnded:Connect(function(touched)
      local player = game.Players:GetPlayerFromCharacter(touched.Parent)
      if player then

      end
end)
Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Hey gitrog,

I am not sure if my solution is the most efficient but, I sure as heck know that it can suffice for what you are attempting to accomplish. I would say you should put another invisible brick at the door but, a bit more forward so that it's only for people leaving, not for people entering. If the person is entering they should hit this brick before they do the one that gives them the gui and when they are leaving, they should hit the one that gives them the gui before they hit this one. With the invisible brick that is meant to remove the gui, I would put inside it a script that checks if the gui is there in the first place, and if it is there then it would Destroy it. Below is a personal example of what I'm talking about.

Personal Example:

local part = script.Parent; -- The part that will be touched.

part.Touched:Connect(function(obj) -- The anonymous function connected to a Touched event.
    local hum = obj.Parent:FindFirstChild("Humanoid"); -- Variable to check if there's a humanoid.
        if hum then -- Checks if there is a humanoid.
             local char = obj.Parent; -- Sets a variable for the character.
             local plr = game:GetService("Players"):GetPlayerFromCharacter(char); -- Sets a variable for the player.
             local plrgui = plr:WaitForChild("PlayerGui"); -- Sets a variable for the PlayerGui, where the Shop's Gui must reside
             local shop_gui = plrgui:FindFirstChild("THE SHOP GUI'S NAME"); -- Variable for the shop gui(If there is one);
             if shop_gui then -- Checks if there is a shop gui.
                    shop_gui:Destroy(); -- Destroys the shop gui.
             end -- end for the shop gui if statement
       end -- end for the checking humanoid if statement.
end) -- end for the anonymous function.

Well, I hope I helped in one way or another. Have a great day/night.

~~ KingLoneCat

Answer this question