How to tell when the player of a tool leaves?

  1. What do you want to achieve? I want to know when the player leaves/reset so that I can despawn a car.

  2. What is the issue? I am having trouble trying to find ways to do so.

  3. What solutions have you tried so far? Alot of the solutions didn’t work for my case.

local Tool = script.Parent
local debounce = false
local player = script.Parent.Parent
local Players = game:GetService("Players")

script.Parent.Activated:Connect(function()
	if debounce == false then
		
		debounce = true
			
	local Build = game.ReplicatedStorage.MailTruck:Clone()
	for i,v in pairs(game.Workspace:GetChildren()) do

		if v.Name == Tool.Parent.Name.."'s MailTruck" then
			v:Destroy()

		end
	end
	Build.Parent = game.Workspace
	Build:SetPrimaryPartCFrame(Tool.Parent.HumanoidRootPart.CFrame + Vector3.new(15,0,0))
	Build.Name = Tool.Parent.Name.."'s ".."MailTruck"
		wait(1)
		debounce = false
	end
end)

Anyone know what to do? This is a server script.

Im starting to think this is impossible…

Use the PlayerRemoving event in the players service to detect whenever a player leaves
And to detect when a player dies, use the Humanoid.Died event

1 Like

But thats a player. I want the player who left.

The first parameter in the PlayerRemoving event gives the player who left

Made some changes to the code:

local Tool = script.Parent
local debounce = false
local char
local Players = game:GetService("Players")
local playerofthistool

script.Parent.Activated:Connect(function()
	char = script.Parent.Parent
	playerofthistool = Players:GetPlayerFromCharacter(char)
	if debounce == false then
		
		debounce = true
			
	local Build = game.ReplicatedStorage.MailTruck:Clone()
	for i,v in pairs(game.Workspace:GetChildren()) do

		if v.Name == Tool.Parent.Name.."'s MailTruck" then
			v:Destroy()

		end
	end
	Build.Parent = game.Workspace
	Build:SetPrimaryPartCFrame(Tool.Parent.HumanoidRootPart.CFrame + Vector3.new(15,0,0))
	Build.Name = Tool.Parent.Name.."'s ".."MailTruck"
		wait(1)
		debounce = false
	end
	
end)

Players.PlayerRemoving:Connect(function(supposedplayer)
	if supposedplayer == playerofthistool then
		print('Hooplah!')
		for i,v in pairs(game.Workspace:GetChildren()) do

			if v.Name == playerofthistool.Name.."'s MailTruck" then
				v:Destroy()

			end
		end
	end
end)

uhh its not working, do you know why

.PlayerAdded and .PlayerRemoving usually don’t work in the studio. Try in a live game.

You can try this
Also are there any errors?

local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Tool = script.Parent
local debounce = false

local char
local playerofthistool

local function DestroyPlayersTruck(player : Player)
	local truck = workspace:FindFirstChild(player.Name.."'s MailTruck")

	if truck then
		truck:Destroy()
		truck = nil
	end
end


Tool.Activated:Connect(function()
	char = script.Parent.Parent
	playerofthistool = Players:GetPlayerFromCharacter(char)
	
	if not debounce then
		debounce = true
		
		DestroyPlayersTruck(playerofthistool)
		
		
		local Build = ReplicatedStorage.MailTruck:Clone()
		Build.Parent = workspace
		Build:PivotTo(Tool.Parent.HumanoidRootPart.CFrame + CFrame.new(15, 0, 0))
		Build.Name = char.Name.."'s ".."MailTruck"
		
		task.delay(1, function()
			debounce = false
		end)
	end

end)


Players.PlayerRemoving:Connect(function(player)
	if player == playerofthistool then
		DestroyPlayersTruck(player)
	end
end)

it would be easier to just do this

game.Players.PlayerRemoving:Connect(function(Player)
	for i,v in pairs(game.Workspace:GetChildren()) do 
		if v.Name == Player.Name.."'s MailTruck" then
			v:Destroy()
		end
	end
end)