I’m trying to get the player’s character after they leave for a tool system.
I’m wondering if it’s possible to get the character after the player leaves, I’ve tried a few different methods and I either don’t get anything or i get the error: ServerScriptService.Script:7: attempt to index nil with 'FindFirstChild'
I’ve looked around on the DevForum, there’s been 2 posts that were relevant to my issue, one of them had a solution that didn’t work for me, and the other hasn’t been solved for 2 years.
local keyModule = require(game.ServerScriptService:WaitForChild("Game Logic").KeyModule)
game.Players.PlayerRemoving:Connect(function(player)
local char = player.Character
print("Player left")
if char:FindFirstChild("HumanoidRootPart") then
if player:FindFirstChild("Contestant") and game.Workspace:FindFirstChild("Map") then -- added
keyModule.ResetTools(player,game.Workspace.Map)
print("Tools dropped")
end
if player:FindFirstChild("Contestant") then
player.Contestant:Destroy()
elseif player:FindFirstChild("Piggy") then
player.Piggy:Destroy()
end
end
end)
I believe Player.Character will always be nil by the time Players.PlayerRemoving is called.
Maybe try using Player.CharacterAdded or Player.CharacterRemoving event to store a reference of the character instance when they spawn or despawn. Then when PlayerRemoving is fired, you can use that reference instead of trying to get it through player.Character