Touched event glitchy/broken?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a ATM gui where if you touch a part, it moves the frame to your screen using Tweens, but the problem here is that the GUI seems to be glitchy,
    and it keeps entering/exiting the screen even though I’m just touching it.

  2. What is the issue? Include screenshots / videos if possible!

This is a visual representation of what keeps happening. ^
Here is the touched script.

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent.Humanoid
	local plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	plr.PlayerGui.Bank.Frame.Visible = true
	plr.PlayerGui.Bank.Frame:TweenPosition(UDim2.new(0.217, 0, 0.031, 0), "Out", "Linear", 0.5)
	wait(0.3)
end)

script.Parent.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent.Humanoid
	local plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	plr.PlayerGui.Bank.Frame:TweenPosition(UDim2.new(0.217, 0, -2, 0), "Out", "Linear", 0.5)
	wait(0.3)
	plr.PlayerGui.Bank.Frame.Visible = false
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried removing the tweens and making the gui visible/not visible when you touch or stop touching it, but the same things happen. I don’t have too much experience with Touched events but if anybody could help it would be really appreciated!!

Using the TouchEnded Event can work extremely wonky & fragile, just like the regular touched event so I wouldn’t recommend using that

What you could do instead however, is either use Magnitude through a loop or you can use Region3 :thinking: I’ll give an example for Magnitude:

Example:

local BankPart = workspace.BankPart

while true do
    for _, Target in pairs(game.Players:GetPlayers()) do
        local Character = Target.Character
        local Gui = Target:WaitForChild("PlayerGui")
        if Character then
            if (Character.HumanoidRootPart.Position - BankPart.Position).Magnitude < 10 then
                Gui.Bank.Frame:TweenPosition(UDim2.new(0.217, 0, 0.031, 0), "Out", "Linear", 0.5)
            else
                Gui.Bank.Frame:TweenPosition(UDim2.new(0.217, 0, -2, 0), "Out", "Linear", 0.5)
            end
        end
    end
    wait(1)
end
1 Like

Thank you sm! I’ll use this method later on now. :grin:

1 Like