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:
local Pad = script.Parent local Debounce = false local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvents = ReplicatedStorage:WaitForChild("Remote Events (Main)") local IsClaimed = Pad.Parent.IsClaimed local Aquariums = workspace.Map.Aquariums Pad.Touched:Connect(function(hit) local Player = Players:GetPlayerFromCharacter(hit.Parent) local Character = Player.Character local ClaimedValue = Player.PlayerGui.AquariumClaimed if not (Debounce) then Debounce = true if IsClaimed.Value == true then return end else if ClaimedValue.Value == true then else Aquariums.PlayerPad1:FindFirstChild("Name Tag").NameUI.Username.Text = hit.Parent.Name RemoteEvents.Data:FireClient(Player) script.Disabled = true script.Parent.Process.Disabled = false ClaimedValue.Value = true IsClaimed.Value = true end wait(1) Debounce = false if IsClaimed.Value == true and Player then Players.PlayerRemoving:Connect(function(LocalPlayer) Aquariums.PlayerPad1:FindFirstChild("Name Tag").NameUI.Username.Text = "Not Claimed" IsClaimed.Value = false end) else return end end end)
I am unsure what is causing this issue, and would like some help. Thanks
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.
local Players = game:GetService("Players") Pad.Touched:Connect(function(hit) local Player = Players:GetPlayerFromCharacter(hit.Parent) if Player then Players.PlayerRemoving:Connect(function(PlayerLeaving) if PlayerLeaving == Player then --the player that left the game is the player that touched the pad end end) end end)
Thats easy
game.Players.PlayerRemoving:Connect(function(player) print(player.Name) end)
Comment if there are any bugs