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

Detect if a specific player has left the game?

Asked by 4 years ago

Hello.

I am currently experimenting with the Players.PlayerRemoved event, and I was wondering, is it possible for the game to detect if a specific player has left the game, from a Player gotten from the GetPlayerFromCharacter(hit.Parent) function. I have attempted this in a script, and what's supposed to happen is that when the player claims an aquarium, it sets the pad text to their name, and makes it unclaimable afterwards. I want to make it so that when the player leaves, the aquarium frees up and another player can claim it. I have attempted to use the Players.PlayerRemoved event with a specific player but it has not worked.

Here is my script:

01local Pad = script.Parent
02local Debounce = false
03 
04local Players = game:GetService("Players")
05local ReplicatedStorage = game:GetService("ReplicatedStorage")
06 
07local RemoteEvents = ReplicatedStorage:WaitForChild("Remote Events (Main)")
08local IsClaimed = Pad.Parent.IsClaimed
09 
10local Aquariums = workspace.Map.Aquariums
11 
12Pad.Touched:Connect(function(hit)
13 
14    local Player = Players:GetPlayerFromCharacter(hit.Parent)
15    local Character = Player.Character
View all 42 lines...

I am unsure what is causing this issue, and would like some help. Thanks

0
Put the Player Removing OUTSIDE of the touched event. Dovydas1118 1495 — 4y
0
How would I go to make sure it fires only to a specific player leaving, in my case the player that has hit the part RazzyPlayz 497 — 4y
0
You'd need to define a blank variable before the Touched function and assign it to the player after the part is touched. Only then do you check to see if that specific player leaves. DeceptiveCaster 3761 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

The PlayerRemoving event has a parameter that details the player that is leaving the game. We can simply check to see if the parameter is the same as the player that touched the pad.

01local Players = game:GetService("Players")
02 
03Pad.Touched:Connect(function(hit)
04 
05    local Player = Players:GetPlayerFromCharacter(hit.Parent)
06    if Player then
07        Players.PlayerRemoving:Connect(function(PlayerLeaving)
08            if PlayerLeaving == Player then
09                --the player that left the game is the player that touched the pad
10            end
11        end)
12    end
13 
14end)
1
Wrapping a script signal in another script signal usually isn't a great idea. This is a good answer, but it isn't good code. DeceptiveCaster 3761 — 4y
0
You're right, I think a better way would be to have a script that checks all Aquariums when a player leaves and would set their claimed value to false if the player that left was their owner. virushunter9 943 — 4y
Ad
Log in to vote
-2
Answered by
MattVSNNL 620 Moderation Voter
4 years ago

Thats easy

1game.Players.PlayerRemoving:Connect(function(player)
2    print(player.Name)
3end)

Comment if there are any bugs

Answer this question