local M={}M.cr=coroutine.resume M.cc=coroutine.create M.crcc=function(M)return coroutine.resume(coroutine.create(M))end M.FindFirstChildWithAttribute=function(M,D,e,k)if type(M)=="\116\097\098\108\101"then M=D D=e end for M,n in pairs(M:GetChildren())do if n:GetAttribute(D)then if e==true then return n:GetAttribute(D)else if k and n:GetAttribute(D)==k then return n end end end end return nil end M.FindFirstChildWithPropertyValue=function(M,D,e)for M,k in pairs(M:GetChildren())do if k[D]and k[D]==e then return k end end return nil end M.GlobalTrackSwitch=function(D,e,k,n)local i=M.FindFirstChildWithPropertyValue(workspace.GlobalSounds,"\080\108\097\121\105\110\103",true)local X=workspace.GlobalSounds:FindFirstChild(D)local x=X.Volume if X.Playing==true then return else X:Play()X.Volume=0 end local P=TweenService:Create(i,TweenInfo.new(e),{Volume=0})local r=TweenService:Create(X,TweenInfo.new(e),{Volume=x})P:Play()r:Play()M.crcc(function()task.wait(e)i:Stop()end)end M.TextTransition=function(M,D,e,k,n)local i=M[D]local X=string.len(i)local x=string.len(e)local P=k or 1 local r=n or"\067\111\108\108\097\112\115\101"if r=="\067\111\108\108\097\112\115\101"then for e=2,X/2,1 do M[D]=string.sub(i,e,X-(e-1))task.wait((P/2)*.25)end M[D]=" "task.wait((P/2)*.25)for k=x/2,0,-1 do M[D]=string.sub(e,k,x-(k-1))task.wait((P/2)*.25)end elseif r=="\083\099\114\097\109\098\108\101"then end end return M



local Utilities = {}

Utilities.cr = coroutine.resume
Utilities.cc = coroutine.create
Utilities.crcc = function(func)
	return coroutine.resume(coroutine.create(func))
end

Utilities.FindFirstChildWithAttribute = function(Parent, AttributeName, ReturnAttributeValue, Value)
	
	if type(Parent) == "table" then
		Parent = AttributeName
		AttributeName = ReturnAttributeValue
	end
	
	for _, Child in pairs(Parent:GetChildren()) do
		if Child:GetAttribute(AttributeName) then
			if ReturnAttributeValue == true then
				return Child:GetAttribute(AttributeName)
			else
				if Value and Child:GetAttribute(AttributeName) == Value then
					return Child
				--else
				--	return Child -- Why?
				end
			end
		end
	end

	return nil
end

Utilities.FindFirstChildWithPropertyValue = function(Parent, Property, Value)
	for _, Child in pairs(Parent:GetChildren()) do
		if Child[Property] and Child[Property] == Value then
			return Child
		end
	end

	return nil
end

Utilities.GlobalTrackSwitch = function(NewTrack, FadeTime, ClientOnly, Player)

	-- Find what Sound in workspace.GlobalSounds is playing, tween fade it out and tween fade in NewTrack

	-- Find the current track
	local CurrentTrack = Utilities.FindFirstChildWithPropertyValue(workspace.GlobalSounds, "Playing", true)
	local NewTrack = workspace.GlobalSounds:FindFirstChild(NewTrack)
	local NewTrackInitVol = NewTrack.Volume

	--print(CurrentTrack)

	-- If the new track is already playing, return
	if NewTrack.Playing == true then
		return
	else
		NewTrack:Play()
		NewTrack.Volume = 0
	end

	-- Fade out current track and fade in new track
	local CurrentTrackFade = TweenService:Create(CurrentTrack, TweenInfo.new(FadeTime), {Volume = 0})
	local NewTrackFade = TweenService:Create(NewTrack, TweenInfo.new(FadeTime), {Volume = NewTrackInitVol})
	CurrentTrackFade:Play()
	NewTrackFade:Play()

	Utilities.crcc(function()
		task.wait(FadeTime)
		CurrentTrack:Stop()
	end)

end

Utilities.TextTransition = function(Object, TextProperty, EndText, Speed, TransitionType)
	
	local StartText = Object[TextProperty]
	local StartTextLength = string.len(StartText)
	local EndTextLength = string.len(EndText)

	local Speed = Speed or 1
	local TransitionType = TransitionType or "Collapse"

	if TransitionType == "Collapse" then
		-- Take one letter from beginning and end until no letters are left, then do the opposite with EndText
		
		for i=2, StartTextLength/2 do  -- Collapse
			Object[TextProperty] = string.sub(StartText, i, StartTextLength-(i-1))
			task.wait(Speed/2*.25)
		end
		
		Object[TextProperty] = " "
		task.wait(Speed/2*.25)

		for  i=EndTextLength/2, 0, -1 do  -- Expand from middle
			Object[TextProperty] =  string.sub(EndText, i, EndTextLength-(i-1))
			task.wait(Speed/2*.25)
		end

	elseif TransitionType == "Scramble" then
		-- Randomly swap out letters from StartText to EndText

	end
end

return Utilities
