Script not working

My script is not working, Its a tycoon one

The fade doesnt work

Without further ado here it is

local tS = game:GetService("TweenService")
local tI = TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false, 0)

local GoalsTransparency0 = {
	
	Transparency = 0
	
}

local GoalsTransparency1 = {

	Transparency = 1

}

local BoolVal = script.Parent.Buyable

script.Parent.Head.Touched:Connect(function(hit)
if script.Parent.Buyable.Value == true then
	
		
		if hit.Parent:FindFirstChild("Humanoid") then
			
			if hit.Parent:FindFirstChild("Owns") then
				
				if hit.Parent.Owns.Value == script.Parent.Parent.Parent.Name then
					
					BoolVal.Value = false
					
				end
				
			end
			
		end
		
	end
	
end)

print("hi")


function Check()

	if BoolVal.Value == true then
	
		for _, v in pairs(script.Parent.Head:GetChildren()) do
			
			if v then
				
				local Tween1 = tS:Create(script.Parent.Head.BillboardGui2.Text, tI, {TextTransparency = 0})

				Tween1:Play()

				local Tween2 = tS:Create(script.Parent.Head.BillboardGui1.Frame, tI, {ImageTransparency = 0})

				Tween2:Play()

				local Tween3 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame, tI, {ImageTransparency = 0})

				Tween3:Play()

				local Tween4 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame.Text, tI, {TextTransparency = 0})

				Tween4:Play()
				
				if v:IsA("UnionOperation") or v:IsA("MeshPart") then
					
					local TweenTransparency0 = tS:Create(v, tI, GoalsTransparency0)
					
					TweenTransparency0:Play()
					
				end
				
				for _, i in pairs(script.Parent.Head.Glow:GetChildren()) do

					if i:IsA("Decal") then

						local TweenTransparency0 = tS:Create(i, tI, GoalsTransparency0)

						TweenTransparency0:Play()

					end

				end
				
				
				if script.Parent:FindFirstChild("Head") then
					
					local TweenTransparency0 = tS:Create(script.Parent.Head, tI, GoalsTransparency0)

					TweenTransparency0:Play()
					
				end

				
				
			end
			
		end
		
		
	else
		
		for _, v in pairs(script.Parent.Head:GetChildren()) do

			if v then

				if v:IsA("UnionOperation") or v:IsA("MeshPart") then

					local TweenTransparency1 = tS:Create(v, tI, GoalsTransparency1)

					TweenTransparency1:Play()

				end

				if v:FindFirstChildWhichIsA("Decal") or v:FindFirstChildWhichIsA("Texture") then

					local TweenTransparency1 = tS:Create(v:FindFirstChildWhichIsA("Decal") or v:FindFirstChildWhichIsA("Texture"), tI, GoalsTransparency1)

					TweenTransparency1:Play()

				end
				
				local Tween1 = tS:Create(script.Parent.Head.BillboardGui2.Text, tI, {TextTransparency = 1})

				Tween1:Play()

				local Tween2 = tS:Create(script.Parent.Head.BillboardGui1.Frame, tI, {ImageTransparency = 1})

				Tween2:Play()

				local Tween3 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame, tI, {ImageTransparency = 1})

				Tween3:Play()

				local Tween4 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame.Text, tI, {TextTransparency = 1})

				Tween4:Play()


			end

		end
	
	end
	
end

print("hi2")

while wait() do
	
	Check()
	
end

print("hi3")
1 Like

The fade isn’t working because you’re currently replaying the Tweens every time the for loop iterates

The for loop is inside of a function that’s being called in an infinite loop, so the Tweens never have a chance to complete running

1 Like

thats weird because it prints all the hi’s

That is because it is not erroring, its just simply not completing the tweens intime before the next is played

1 Like
local Tween = TweenService:Create(Obj, Info, Properties)

Tween.Completed:Connect(function()
    -- Do whatever after tween has finished
end)

-- OR

Tween.Completed:Wait()

This is unrelated to your problem, but I’m quite sure that “hi3” won’t be printed since it’s blocked by the while loop that’s right above it due to the fact that the loop isn’t running in a separate thread


@rusted010101 This is admittedly a quick fix so it might not work, but you’ll need to do something like this to wait for the Tweens to finish playing before the loops continue:

local tS = game:GetService("TweenService")
local tI = TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false, 0)

local GoalsTransparency0 = {

	Transparency = 0

}

local GoalsTransparency1 = {

	Transparency = 1

}

local BoolVal = script.Parent.Buyable

script.Parent.Head.Touched:Connect(function(hit)
	if script.Parent.Buyable.Value == true then


		if hit.Parent:FindFirstChild("Humanoid") then

			if hit.Parent:FindFirstChild("Owns") then

				if hit.Parent.Owns.Value == script.Parent.Parent.Parent.Name then

					BoolVal.Value = false

				end

			end

		end

	end

end)

print("hi")


function Check()

	if BoolVal.Value == true then

		for _, v in pairs(script.Parent.Head:GetChildren()) do

			if v then

				local Tween1 = tS:Create(script.Parent.Head.BillboardGui2.Text, tI, {TextTransparency = 0})

				Tween1:Play()

				local Tween2 = tS:Create(script.Parent.Head.BillboardGui1.Frame, tI, {ImageTransparency = 0})

				Tween2:Play()

				local Tween3 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame, tI, {ImageTransparency = 0})

				Tween3:Play()

				local Tween4 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame.Text, tI, {TextTransparency = 0})

				Tween4:Play()
				Tween4.Completed:Wait()

				if v:IsA("UnionOperation") or v:IsA("MeshPart") then

					local TweenTransparency0 = tS:Create(v, tI, GoalsTransparency0)

					TweenTransparency0:Play()
					TweenTransparency0.Completed:Wait()

				end

				for _, i in pairs(script.Parent.Head.Glow:GetChildren()) do

					if i:IsA("Decal") then

						local TweenTransparency0 = tS:Create(i, tI, GoalsTransparency0)

						TweenTransparency0:Play()
						TweenTransparency0.Completed:Wait()

					end

				end


				if script.Parent:FindFirstChild("Head") then

					local TweenTransparency0 = tS:Create(script.Parent.Head, tI, GoalsTransparency0)

					TweenTransparency0:Play()
					TweenTransparency0.Completed:Wait()

				end



			end

		end


	else

		for _, v in pairs(script.Parent.Head:GetChildren()) do

			if v then

				if v:IsA("UnionOperation") or v:IsA("MeshPart") then

					local TweenTransparency1 = tS:Create(v, tI, GoalsTransparency1)

					TweenTransparency1:Play()
					TweenTransparency1.Completed:Wait()

				end

				if v:FindFirstChildWhichIsA("Decal") or v:FindFirstChildWhichIsA("Texture") then

					local TweenTransparency1 = tS:Create(v:FindFirstChildWhichIsA("Decal") or v:FindFirstChildWhichIsA("Texture"), tI, GoalsTransparency1)

					TweenTransparency1:Play()
					TweenTransparency1.Completed:Wait()

				end

				local Tween1 = tS:Create(script.Parent.Head.BillboardGui2.Text, tI, {TextTransparency = 1})

				Tween1:Play()

				local Tween2 = tS:Create(script.Parent.Head.BillboardGui1.Frame, tI, {ImageTransparency = 1})

				Tween2:Play()

				local Tween3 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame, tI, {ImageTransparency = 1})

				Tween3:Play()

				local Tween4 = tS:Create(script.Parent.Head.BillboardGui1.Frame.Frame.Text, tI, {TextTransparency = 1})

				Tween4:Play()
				Tween4.Completed:Wait()


			end

		end

	end

end

print("hi2")

task.spawn(function()
	while true do

		Check()

		task.wait()

	end
end)

print("hi3")

Essentially I added .Completed:Wait() like @x6nnx suggested. Using :Wait instead of :Connect allows you to wait for an event to fire without having to worry about memory leaks due to connections