I only want my gui to show when the part is being touched, right now it is staying on the screen until you X out of it. This is what I have using a remote event(probably wrong thing to use)
local limit = 2 script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player.leaderstats.Points.Value >= limit then if hit.Parent:FindFirstChild("Torso") then script.Parent.CanCollide = false elseif hit.Parent:FindFirstChild("UpperTorso") then script.Parent.CanCollide = false end end end end)
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid")then game.ReplicatedStorage.Popup:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent)) end end)
Your solution to this is TouchEnded. CanCollide has nothing to do with stopping a touched event, so don't use that. This is what you should be using
Part.Touched:Connect(function(hit) if game.Players:FindFirstChild(hit.Parent.Name) then -- Fire a client event to pop up a GUI end end) Part.TouchEnded:Connect(function(hit) if game.Players:FindFirstChild(hit.Parent.Name) then --Close the GUI with a client event end end)
A better solution would be to use magnitude from a local script. It will work nicely if it's a cylinder too. That would work like this
local RunService = game:GetService('RunService') local Range = 10 --Distance in studs local Part = game.Workspace.Part --Part that you want to detect when touched local LocalPlayer = game.Players.LocalPlayer repeat wait() until LocalPlayer.Character local PlayerCharacter = LocalPlayer.Character local HumanoidRootPart = PlayerCharacter:WaitForChild('HumanoidRootPart') RunService.Heartbeat:Connect(function() --fires every frame/FPS if (HumanoidRootPart.Position - Part.Position).magnitude < Range then --Pull up the GUI from here else --Close the GUI here end end)
Hope this helps out.