What is the best method to use wait?

Hello, I am a new member here. I’m having an issue with the model fading. I want the model to turn off cancollide when transparency is entirely invisible. But one problem when I put tween completed wait. It does like this. Video Gyazo - Sorry I don’t know how to upload the video into this forum. I’ll use the link gyazo for now.

the script

local player = game:GetService("Players").LocalPlayer
local fadingfolder = workspace.FadingParts

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(1)

local toggle = false


local function touched(part)
	local model = part.Parent
	part.Touched:Connect(function(hit)
		if toggle == false then
			local function faded(fadepart)
				if fadepart.Name ~= "TouchPart" then
					local goal = {
						Transparency = 1
					}
					local tween = tweenservice:Create(fadepart, info, goal)
					tween:Play()
					tween.Completed:Wait()
					fadepart.CanCollide = false
				end
			end
			
			local function fadeback(fadepart)
				if fadepart.Name ~= "TouchPart" then
					local goal = {
						Transparency = 0
					}
					local tween = tweenservice:Create(fadepart, info, goal)
					tween:Play()
					tween.Completed:Wait()
					fadepart.CanCollide = true
				end
			end
			
			
			for _, fadepart in pairs(model:GetChildren()) do
				faded(fadepart)
			end
			
			wait(5)
			
			for _, fadepart in pairs(model:GetChildren()) do
				fadeback(fadepart)
			end
		end
	end)
end



for _, model in pairs(fadingfolder:GetChildren()) do
	local part = model:WaitForChild("TouchPart")
	touched(part)
end
1 Like

Instead of using the :Wait() event, connect it to a function instead as calling :Wait() causes the script to entirety yield its thread, and prevent it from doing anything else until the Tween is completed

local player = game:GetService("Players").LocalPlayer
local fadingfolder = workspace.FadingParts

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(1)

local toggle = false


local function touched(part)
	local model = part.Parent
	part.Touched:Connect(function(hit)
		if toggle == false then
			local function faded(fadepart)
				if fadepart.Name ~= "TouchPart" then
					local goal = {
						Transparency = 1
					}
					local tween = tweenservice:Create(fadepart, info, goal)
					tween:Play()
					
					tween.Completed:Connect(function()
						fadepart.CanCollide = false
					end)
				end
			end
			
			local function fadeback(fadepart)
				if fadepart.Name ~= "TouchPart" then
					local goal = {
						Transparency = 0
					}
					local tween = tweenservice:Create(fadepart, info, goal)
					tween:Play()
					tween.Completed:Connect(function()
						fadepart.CanCollide = true
					end)
				end
			end
			
			
			for _, fadepart in pairs(model:GetChildren()) do
				faded(fadepart)
			end
			
			task.wait(5)
			
			for _, fadepart in pairs(model:GetChildren()) do
				fadeback(fadepart)
			end
		end
	end)
end

for _, model in pairs(fadingfolder:GetChildren()) do
	local part = model:WaitForChild("TouchPart")
	touched(part)
end

Cause now, we’re creating a function for each individual Tween once its completed, so that every single Part can be non-collidable

Also, consider using task.wait(5) instead as the old wait() method shouldn’t be used anymore & it doesn’t properly yield on certain occasions :wink:

1 Like

Oh, thanks but seem like tween completed is not working. The video https://gyazo.com/fa2e07fb7ccac4c78eda3dadcc2d6dd7

It shows that model did turn off cancollide before the model becomes entirely invisible

After doing a bit of configuration, you’re never setting the toggle variable to true so what I think is actually happening here, is that the Tween keeps getting cancelled every time you touch the Model of the Part, and that also detects the .Completed Event as well

Just make sure to set individual toggle variables within your touched function, so that each individual one can’t fire multiple times:

local player = game:GetService("Players").LocalPlayer
local fadingfolder = workspace.FadingParts

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(1)

local function faded(fadepart)
	if fadepart.Name ~= "TouchPart" then
		local goal = {
			Transparency = 1
		}
		local tween = tweenservice:Create(fadepart, info, goal)
		tween:Play()
	
		tween.Completed:Connect(function()
			fadepart.CanCollide = false
		end)
	end
end

local function fadeback(fadepart)
	if fadepart.Name ~= "TouchPart" then
	    local goal = {
			Transparency = 0
		}
		local tween = tweenservice:Create(fadepart, info, goal)
		tween:Play()

		tween.Completed:Connect(function()
			fadepart.CanCollide = true
		end)
	end
end

local function touched(part)
	local toggle = false
	local model = part.Parent

	part.Touched:Connect(function(hit)
		if toggle then return end
		
		toggle = true
		
		for _, fadepart in pairs(model:GetChildren()) do
			faded(fadepart)
		end

		task.wait(5)

		for _, fadepart in pairs(model:GetChildren()) do
			fadeback(fadepart)
		end
			
		task.wait(5)

		toggle = false

	end)
end

for _, model in pairs(fadingfolder:GetChildren()) do
	local part = model:WaitForChild("TouchPart")
	touched(part)
end
1 Like

Ah, that’s helpful. No wonders when I added print. It printed many times. It makes me wonder why it’s printing many times. Now I know. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.