Function not stopping

So i want to make when player touches the part it stops the fade part function. if player resets. however, it for some reason wont stop.

local ts=game:GetService("TweenService")
local part=script:FindFirstAncestorWhichIsA("BasePart")
local status=part.Status
local transparency=part.Transparency
local localplr=game.Players.LocalPlayer
local event=Instance.new("BindableEvent",part)
event.Name="reset"
local deb=false
return function()
	local touchedParting=nil
	local function touchedPart()
		local hit=touchedParting
		local plr=game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr==localplr then
			if status.Value=="Normal" and deb==false then
				status.Value="Fading"
				local fadeTween=ts:Create(part,TweenInfo.new(1),{Transparency=1})
				fadeTween:Play()
				task.wait(1)
				part.CanCollide=false
				status.Value="Faded"
				task.wait(3)
				status.Value="Regenerating"
				local unfadeTween=ts:Create(part,TweenInfo.new(1),{Transparency=transparency})
				unfadeTween:Play()
				task.wait(1)
				part.CanCollide=true
				status.Value="Normal"
			end
		end
	end
	local currentSpawn=nil
	part.Touched:Connect(function(hit)
		touchedParting=hit
		currentSpawn=coroutine.create(touchedPart)
		coroutine.resume(currentSpawn)
	end)
	event.Event:Connect(function()
		if currentSpawn then
			coroutine.close(currentSpawn)
			currentSpawn=nil
			print("cancelled")
		end
		part.Transparency=transparency
		part.CanCollide=true
		deb=true
		status.Value="Normal"
		task.delay(1,function()
			deb=false
		end)
	end)
	game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("PlayerReseted").OnClientEvent:Connect(function()
		event:Fire()
	end)
end

Works perfectly fine I don’t see the issue, when the player touches it fades away and reappears after some time.

When player resets, and if player touched the part, the fade function for some reason not stops sometimes.

You should clear the connections after you finish.

The coroutine can’t close if it’s currently running.


Ref: coroutine | Documentation - Roblox Creator Hub

In other words you can’t stop the coroutine from running it’s body by closing it. You can yield it if want it to stop but that’s only within the coroutine’s body.

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