I know what you’re most likely gonna say: a return
. But I wanna explain what I’m trying to accomplish and why I want something a bit better.
I’m making a Turn-Based Combat type of game where players and enemies attack each other with card attacks. Each attack comes with its own script with animations and sounds and camera movements. For example, I have a Sword card where its script has the player pull out a sword, walk up to the enemy, swing it, and then walk back to their sigil. This lasts about 10 seconds. More powerful cards will have longer and more complicated scripts so that they look cooler.
The thing is, I’m trying to handle this case where if a player leaves DURING the animation, the script would error due to the player no longer existing. If the player leaves, I want the script to stop immediately so that the battle can end. So yes, I could use return
, but I’d have to literally do it every line like this:
(Pseudocode)
local move = player:MoveTo(enemy.Position)
if player == nil then
return
end
wait(3)
if player == nil then
return
end
slash()
dealDamage()
wait(1)
if player == nil then
return
end
local move2 = player:MoveTo(player.Position)
wait(3)
if player == nil then
return
end
So, you see my dilemma? I don’t wanna have to put a return
statement after every single line in which the player might disappear due to leaving or losing connection. It makes my code look messy and it can get really repetitive when I’m dealing with bigger scripts for more powerful attacks.
Is there a much nicer, more clean way of handling this?