How do I fire the remote function While waiting for a tween to end

I want to wait for a tween to end so I can delete the Object but if I wait for the tween to end the RemoteFunction isn’t fired

tween =
		TS:Create(
			rootPart,
			tweenInfo,
			{
				CFrame = player.Character.PrimaryPart.CFrame * CFrame.new(0, 0, -100) *
				CFrame.Angles(0, math.rad(90), math.rad(-90))
			}
		):Play()
	return playerPos
end

function deleteWave()
	clonedAbility:Destroy()
end

local deleteWaveCoroutine =
	coroutine.create(
		function()
			deleteWave()
		end
	)

coroutine.resume(deleteWaveCoroutine)
abilityEvent.OnServerInvoke = summonWave

I tried using a coroutine but I’m not 100% sure on how they work. Do I have the right idea? What needs to be fixed?

I’m not too sure what you’re asking here. Could you elaborate on what you’re trying to do and maybe show a bit more of the code? I’m not sure how you’re defining clonedAbility.

I see, I don’t think you should do it relying on variables created from a different function as you’re working with many scopes and you might notice other peoples’ waves will interfere with each other. You should probably do it in order and create the coroutine inside of the function so it doesn’t yield until the tween is over. So something like:

local function summonWave(player, playerPos)
    task.wait(0.5)
    -- continue for rest of function
    -- also ensure you're defining variables inside this function using local
    local tween = TS:Create(...)
    tween:Play()
    coroutine.resume(coroutine.create() -- you can also use task.spawn/task.delay/coroutine.wrap/create a connection using signal:Once for something a little neater
        tween.Completed:Wait()
        rootPart:Destroy()
    end))
    return playerPos
end

remote.OnServerInvoke = summonWave
1 Like

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