How would I stop the function when the player isn't touching anymore?

Hey,

so I want that if the player doesn’t touch anymore the function stops. And if the player touches again it starts from the beginning. But somehow, the BillboardGUI is going invisible but the part still disappears after waiting 5 seconds.

local Players = game:GetService("Players")

local Box1 = script.Parent
local Prize = Box1.Value
local BillboardGui = script.Parent.Parent.BillboardPart.BillboardGui
local Debounce = false

local function tweenCallback(tweenCompleted)
	if tweenCompleted then
		print("Success!")
	else
		print("Fail!")
	end
end

local function onBoxTouched(hit)
	if Debounce then
		return
	end

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		Debounce = true
		local leaderstats = player:WaitForChild("leaderstats")
		leaderstats.Money.Value += Prize.Value
		leaderstats.Food.Value += 5
		BillboardGui.Enabled = true
		local connection;
		connection = Box1.TouchEnded:Connect(function()
			wait(0.2)
			BillboardGui.Enabled = false
			BillboardGui.Frame.InsideFrame:TweenSize(UDim2.new(0, 10,1, 0), "Out", "Linear", 5, true)
			connection:Disconnect()
		end)
		--[[if game.Players:GetPlayerFromCharacter(hit.Parent):DistanceFromCharacter(Box1.Position) > 5 then 
			BillboardGui.Enabled = false
			BillboardGui.Frame.InsideFrame:TweenSize(UDim2.new(0, 10, 1, 0), "Out", "Linear", 5, false, tweenCallback)
			
		end ]]
		BillboardGui.Frame.InsideFrame:TweenSize(UDim2.new(0, 200, 1, 0), "Out", "Linear", 5, false, tweenCallback)
		task.wait(5)
		BillboardGui.Enabled = false
		Box1.Transparency = 1
		Box1.CanCollide = false
		BillboardGui.Frame.InsideFrame:TweenSize(UDim2.new(0, 10, 1, 0), "Out", "Linear", 5, false, tweenCallback)
		task.wait(10)
		Box1.Transparency = 0
		Box1.CanCollide = false
		Debounce = false
			
	end
end

Box1.Touched:Connect(onBoxTouched)
1 Like

Box1.TouchEnded:Connect(function)

I know about that, but how can I stop the touched function?

Have you tried ‘break’? It may be working like that.

Where should I add the break? In the connection?

make a variable add if statement just below the touch function and make it so that it let it go if our variable is false if not then it wont proceed

But what if the function already runs when the touch is ended?

Hello, you can try disconnecting the event inside of TouchEnded, the touched event will no longer execute as it is not being listened to after being disconnected.

local touchConnection = nil

touchConnection = Box1.Touched:Connect(onBoxTouched)


Box1.TouchEnded:Connect(function()

if touchConnection then
   touchConnection:Disconnect()
end

end)

Hope this gives you an idea.

As for this, you can make a variable like so

local isTouching = false

Box1.Touched:Connect(function()
       isTouching = true
       task.wait(5)
       if not isTouching then return end 
       print("Hi")
end)

Box1.TouchEnded:Connect(function()
      isTouching = false
end)

If the player stops touching the part, it will toggle the bool off, and after the 5 second wait, since the bool is off it will not execute the code.

The problem is if the player touches the part and then stops touching, the function will still run or not? Because the if statement will only check if its touching on the start?

How do I add this to my script?

You can implement the check after every wait, there is no way to like stop a running function completely and reverse it’s effects. You have to do it manually, example piece from your code:

               
local function Reset()
     Box1.Transparency = 0
     Box1.CanCollide = false
     Debounce = false
end

task.wait(5)

if not isTouching then Reset() return end 

BillboardGui.Enabled = false
Box1.Transparency = 1
Box1.CanCollide = false
BillboardGui.Frame.InsideFrame:TweenSize
(
UDim2.new(0, 10, 1, 0), 
"Out", 
"Linear", 
5, 
false,
 tweenCallback)

task.wait(10)

if not isTouching then Reset() return end 

Box1.Transparency = 0
Box1.CanCollide = false

This gives an idea of how it works.

1 Like

That is up to you, I am to only show you the solution, if you need more help, don’t hesitate to reply. Not permitted to entirely write it out for you.

Clone this script with a different script when the object is touched. When the object isn’t touching anymore delete the cloned script. Add a debounce so it doesn’t fire multiple times.

obj.Touched:Connect(function()
     if not debounce then
     debounce = true
     else
     return
     end
     clone = scriptToClone:Clone()
end)

obj.TouchEnded:Connect(function()
     if debounce then
     debounce = false
     else
     return
     end
     clone:Destroy()
end)

Thats actually a good idea, thank you. I will try it out if I can’t fix my script.

1 Like

2 issues:

  1. Sometimes when running inside the part (cancollide off) the Tween just gets cancelled (= TouchEnded)


I tried setting the position of the InsideFrame but somehow it isn’t working.