Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you fix the module not working after the caller leaves?

Asked by 4 years ago

When the caller leaves the module stops the code entirely. For example, if I were to do this in a module

-- The script that calls the module
local Player = game.Players.LocalPlayer
local EnemyPlayer = workspace.EnemyPlayer 
local Module = require(game.ReplicatedStorage.Module)
module.Call(EnemyPlayer)
--Module Call
EnemyPlayer.Humanoid:TakeDamage(20)
wait(20)
EnemyPlayer.Humanoid:TakeDamage(20)

If the Player were the one to call the module and leave between the wait time, the second damage wouldn't happen. How do I fix this?

0
You could also use pcall(function() as that will keep the code running even if an error occurs. killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years ago

Well you can't damage a Player when they leave, but you can avoid the error by checking if the EnemyPlayer exists before damaging them.

if game.Players:FindFirstChild(EnemyPlayer.Name) then
    EnemyPlayer.Humanoid:TakeDamage(20)
end
Ad

Answer this question