Not waiting for Tween to finish playing

I’m experimenting with tweening and selection boxes and I’ve encountered an error.

I’ve tweened the selection boxes transparency to “fade in and out”, but my code is not waiting for the tween to finish, skipping the wait() and changing the Adornee, can someone look at it the code and see what’s wrong:

mouse.Move:Connect(function()
    local Target = mouse.Target
    if equipped then
        if mouse.Target ~= nil then
            local humanoid = mouse.Target.Parent:FindFirstChild("HumanoidRootPart")
            if humanoid then
                if tween and tweening then
                    tween.Completed:Wait()
                end
                SelectionBox.Adornee = humanoid
                tween = TweenService:Create(SelectionBox, TweenInfo.new(1), {Transparency = 0.3})
                tween:Play()
            else
                tween = TweenService:Create(SelectionBox, TweenInfo.new(1), {Transparency = 1})
                tweening = true
                tween:Play()
                tween.Completed:Wait()
                SelectionBox.Adornee = nil
                tweening = false
            end
        end
    end
end)

Hey!
Have you tried

tween.Completed:Connect(function()
    SelectionBox.Adornee = nil tweening = false
end)

? It may work better, then tween.Completed:Wait(). Try it out and get back to us.

I think it isn’t waiting since you put the :Wait() inside of if tween and tweening. One of those values probably isn’t true, so the :Wait() never happens.

On a side note, I think that mouse event is deprecated, so you should use UserInputService instead of mouse.

@EgoMoose made a handy module which you can treat like the player mouse, but it uses UserInputService internally. Here’s the link: UserInputService Mouse

With a bit of research and tampering, I’ve figured out the problem,

mouse.Move:Connect(function()
    local Target = mouse.Target
    if equipped then
        if mouse.Target ~= nil then
            local humanoid = mouse.Target.Parent:FindFirstChild("HumanoidRootPart")
            if humanoid then
                SelectionBox.Adornee = humanoid
                tween = TweenService:Create(SelectionBox, TweenInfo.new(1), {Transparency = 0.3})
                tween:Play()
            else
                tween = TweenService:Create(SelectionBox, TweenInfo.new(1), {Transparency = 1})
                tween:Play()
				tween.Completed:Connect(function(playbackState)
					**if playbackState == Enum.PlaybackState.Completed then**
						**SelectionBox.Adornee = nil**
					**end**
				end)
			end
        end
    end
end)