CharacterRemoving from localscript

local Player = game.Players.LocalPlayer

Player.CharacterRemoving:Connect(function()

end)

Does characterremoving not fire from localscripts in startercharacter? If not, is there any alternative?

3 Likes

I guess not! With some testing though, I figured out that this behavior happens because the character’s parent gets set to nil which then prevents the LocalScript from executing further code.

local Player = game.Players.LocalPlayer

Player.CharacterRemoving:Connect(function(character)
	print(character.Parent)
end)

image

Anyways, if you wanted to run this type of script on the client, you could just use a Script whose RunContext is set to Client instead of a LocalScript.

1 Like

Doesnt setting the run context to client just make it a local script ?

1 Like

Not necessarily. LocalScripts are limited in which containers they can be stored in. More specifically, in this case, if the LocalScript is not a descendant of the player’s Character property (their character model), it will not execute:

local Player = game.Players.LocalPlayer

Player.CharacterRemoving:Connect(function(character)
	print(Player.Character == character)
	print(character.Parent)
end)

image

If you change the RunContext property of a Script to Client, it compensates for this – as it allows the script to execute regardless of it’s parent container.

1 Like

Yea i tested it and it worked when the script is placed directly into startercharacterscripts.

However (a bit off topic now), I’m having an issue with characterremoving firing when i parent a localscript to the character via serverscript:

local function GiveLoadout(playerName, toolname)
	local plr = Players:FindFirstChild(playerName)
	if not plr then return end

	local tool = ToolFolder:FindFirstChild(toolname)
	local weaponscript = ScriptFolder:FindFirstChild(toolname .. "Script")
	if not tool then return end

	local clonedTool = tool:Clone()
	local ScriptTool = weaponscript:Clone()
	ScriptTool.Parent = plr.Character
	clonedTool.Parent = plr.Backpack
end
1 Like

Completely disregard what I said earlier. About the script executing regardless of it’s parent container. The script needs to exist in the DataModel (or in other words, it’s parent or ancestor’s parent – in this case – must not be set to nil). The reason why it works when you directly set it under StarterCharacterScripts is because the script inside of the player’s character model is not the one that fires the CharacterRemoving event: it’s the script inside of StarterCharacterScripts that does:

local Player = game.Players.LocalPlayer
local Character = Player.Character

Player.CharacterRemoving:Connect(function(character)
	print(script.Parent)
	print(Player.Character == character)
	print(character.Parent)
end)

image

Which makes sense, I don’t know why I didn’t think about that. For your case, It’s probably best if you manage it in a script that doesn’t become a descendant of nil.

1 Like

I was going to use it in starterplayer but i cant access player.starterplayer

1 Like

What exactly does this LocalScript do? It looks as if you have one for each tool

1 Like

Its just a localscript for a tool (combat)

1 Like

I probably should have specified my question. What does the CharacterRemoving event do in each LocalScript?

1 Like

I’m trying to use it to manage my connections from modules:

Humanoid.Died:Connect(function() --Current
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	maid:DoCleaning()
	hitbox:Destroy()
	camShake:Stop()
	controller:Stop()
	RootGui:Destroy()
end)

Obviously humanoid.died isn’t that effective

I think a way you can keep the CharacterRemoving event inside of a single script would be to create a new Maid object using the player as the key in that LocalScript (parented to StarterPlayerScripts):

-- Assuming player is defined
Maid[Player] = Maid.new()

In each LocalScript, you would just add those objects to the maid via Maid:GiveTask():

-- Each LocalScript
Maid[Player]:GiveTask(hitbox)
-- For camShake, controller, and RootGui as well

So whenever the CharacterRemoving event is fired inside of the single script, you could just run DoCleaning on that Maid object:

Maid[Player] = Maid.new()

Player.CharacterRemoving:Connect(function(character)
    Maid[Player]:DoCleaning()
end)

The only thing you would have to do is rename the Stop function in the camShake and controller modules to Destroy, as the Maid will only “clean” them if they have a Destroy index. If you don’t wanna do that, you can just do:

-- Assume this is a modulescript
function controller:Stop()
   -- Whatever is inside of this
end

controller.Destroy = controller.Stop

Characterremoving doesn’t fire is my only concern

It will if the LocalScript that manages the CharacterRemoving event is inside of StarterPlayerScripts, since it is then cloned and parented to Player.PlayerScripts, which will not be set to nil until the player leaves. What I’m saying is you should have a LocalScript like this:

image

whose contents include:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Maid = require(path.to.maid)

Maid[Player] = Maid.new() -- Construct a new Maid object

Player.CharacterRemoving:Connect(function(character)
    Maid[Player]:DoCleaning()
end)

Let’s say the LocalScripts for each tool had something like:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Maid = require(path.to.maid)
local Hitbox = require(path.to.hitbox)
local camShake = require(path.to.camshake)
local controller = require(path.to.controller)
local RootGui = path.to.RootGui

You can give the Maid that we created in the first LocalScript these tasks:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Maid = require(path.to.maid)
local Hitbox = require(path.to.hitbox)
local camShake = require(path.to.camshake)
local controller = require(path.to.controller)
local RootGui = path.to.RootGui

-- You would then give the maid these tasks
Maid[Player]:GiveTask(Hitbox)
Maid[Player]:GiveTask(camShake)
Maid[Player]:GiveTask(controller)
Maid[Player]:GiveTask(RootGui)

So whenever the CharacterRemoving event fires in the first LocalScript, all connections/instances that you gave the maid are then disconnected/destroyed through DoCleaning:

-- Refer back to first block of code
Player.CharacterRemoving:Connect(function(character)
    Maid[Player]:DoCleaning() -- Destroys Hitbox, camShake, controller, and RootGui
end)

I cant access those modules from another localscript like that? Or can i?

All of what I sent basically just does this: Whenever we create the Maid object, it is stored inside of the Maid module under a key (which is the player instance). A ModuleScript is basically just a table that consists of functions or other values. So yes, you should be able to. Try it and see if it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.