Try this:
And then some code, here is an example for when players join the server: Example script
Then copy and modify it for the leave event too: Source code
Try this:
And then some code, here is an example for when players join the server: Example script
Then copy and modify it for the leave event too: Source code