Tween wait not waiting first time, but then runs again?

I have a video below and it basically shows that when I walk away from the NPC, I get 2 ‘Tween started’ prints and 1 ‘Tween complete’ print instantly (no 5 second wait) and then the second print comes after 5 seconds. The UI isn’t tweening at all, it just deletes instantly

This is the function I call to delete the UI. I don’t know why it’s firing twice tho.

local function DeleteAction()
	local ActionClone = PlayerGui:FindFirstChild('TalkAction')
	if not ActionClone then return end
	
	local ActionTween = TweenService:Create(ActionClone.Control, TweenInfo.new(5), {Size = UDim2.new(0, 0, 0, 0)})
	ActionTween:Play()
	print('Tween started')
	ActionTween.Completed:Wait()
	print('Tween complete')
	ActionClone:Destroy()
end

The function that calls it

local function RenderStepped()
	local LastInput = UserInputService:GetLastInputType()
	NPC = nil
	
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character:FindFirstChild('HumanoidRootPart')
		if not HumanoidRootPart then return end
		
		for _, v in pairs(NPCs:GetChildren()) do
			local Distance = (v.PrimaryPart.CFrame.Position - HumanoidRootPart.CFrame.Position).magnitude
			if not NPC or Distance <= NPC.Distance then
				NPC = {
					Current = v, 
					Distance = Distance
				}
			end
		end
		
		if not NPC then return end
		
		Distance = NPC.Distance
		
		if Distance <= ReachableDistance then
			local DialogueClone = PlayerGui:FindFirstChild('Dialogue')
			if DialogueClone then return end
			
			local ActionClone = PlayerGui:FindFirstChild('TalkAction')
			if ActionClone then return end
			
			local ActionClone = Action:Clone()
			ActionClone.Name = 'TalkAction'
			
			if LastInput == Enum.UserInputType.Touch then
				ActionClone.Control.Tap.Visible = true
				ActionClone.Control.Key:Destroy()
				
				ActionClone.Control.Activated:Connect(function()
					Start()
				end)
			end
			
			ActionClone.Adornee = NPC.Current
			
			ActionClone.Parent = PlayerGui
			
			local ActionTween = TweenService:Create(ActionClone.Control, TweenInfo.new(0.25), {Size = UDim2.new(1, 0, 1, 0)})
			ActionTween:Play()
		else
			DeleteAction() -- Called here
		end
	end
end

Basically deletes it when the player walks away

This is because Tween doesn’t yeilds Therefore it doesn’t gives you any return values so script just runs it and goes on Next Line
You have to add a manual wait there

Wait you actually have a Wait so problem is something else is there any other scripts running?

Can you please read the script next time…

ActionTween.Completed:Wait()

Maybe try adding a debounce for your DeleteAction.

local debounce = false
local function DeleteAction()
	local ActionClone = PlayerGui:FindFirstChild('TalkAction')
	if not ActionClone or debounce then return end -- Added debounce here
	debounce = true
	
	local ActionTween = TweenService:Create(ActionClone.Control, TweenInfo.new(5), {Size = UDim2.new(0, 0, 0, 0)})
ActionTween:Play()
	print('Tween started')
	ActionTween.Completed:Wait()
	print('Tween complete')
	ActionClone:Destroy()
	debounce = false
end

I believe that the reason you’re getting more than one start is because there wasn’t any debounce to stop the function from being run multiple times. By running it multiple times I think that it finished the first tween because it was overridden, causing the completed to fire.

1 Like