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)
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)
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?
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
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)