The attached script seems simple; it is, though I’m having a problem where the while loop waits until the fade function finishes to restart, even though I pcalled it. I’ve tried a lot of different things to fix this but just can’t seem to figure it out. I’m willing to bet that its probably a really simple solution, but I’m having trouble spotting the issue. Any help would be appreciated.
local fade=function(model)
pcall(function()
wait(0.5)
for i=1,20 do
model.surface.Transparency=(i/20)
wait(0.02)
end
model:Destroy()
end)
end
while wait(0.25) do
local c=(script.Parent:Clone())
c.Script:Destroy()
c.surface.Transparency=0
c.Parent=workspace.tmpitems
c.surface.Anchored=true
fade(c)
end
A pcall just catches errors allowing the script to continue running if it encounters one. To let the while loop continue while the function is going you need to use a coroutine.
You’re assigning a function to a variable rather than defining a function which is why it’s pausing there without the function being called yet (also because you have a breakpoint there).
When you call a function, it steps through the whole thing before continuing on in the area it was called. To make it so that the code just starts the function and moves on rather than yield for it, you need to use coroutines.
local fade=coroutine.create(function(model)
wait(0.5)
for i=1,20 do
model.surface.Transparency=(i/20)
wait(0.02)
end
model:Destroy()
end)
while wait(0.25) do
local c=(script.Parent:Clone())
c.Script:Destroy()
c.surface.Transparency=0
c.Parent=workspace.tmpitems
c.surface.Anchored=true
coroutine.resume(fade,c)
end