Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect if a specific player has left the game?

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

3 comments

0

Put the Player Removing OUTSIDE of the touched event.

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

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.

2 answers

✓ Accepted answer

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)

2 comments

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.

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.

Thats easy

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

Comment if there are any bugs

This discussion is preserved as it was. New answers, comments, and votes are closed.