PlayerCharacterDestroyBehavior was recently implemented, which runs Destroy() on any character and player instances when they die or leave. However, I’ve encountered an issue where connections will not disconnect, even after the player has died and respawned, meaning the connection should have disconnected.
On the server, this behavior functions as intended, as you can test with the code attached:
(Script within ServerScriptService)
local Players = game:GetService("Players")
local connection: RBXScriptConnection
local function CharacterAdded(character)
if connection then
print(connection.Connected)
end
connection = character:WaitForChild("Humanoid").Running:Connect(function()
end)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(CharacterAdded)
end)
On the client, if code very similar to that script (modified so it works on the client) is ran within StarterPlayerScripts, the connection will STILL stay connected.
You can see how this behavior works here:
https://gyazo.com/11a24600274bc5135a26434635a57fa3
Reproduction Steps
- Open up a blank baseplate.
- Set PlayerCharacterDestroyBehavior within
Workspace
toEnabled
- Create a
LocalScript
within StarterPlayerScripts - Paste in the following code:
Code and Explanation
local LocalPlayer = game:GetService("Players").LocalPlayer
local connection: RBXScriptConnection
local function CharacterAdded(character)
if connection then
print(connection.Connected)
end
connection = character:WaitForChild("Humanoid").Running:Connect(function(speed)
end)
end
CharacterAdded(LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait())
LocalPlayer.CharacterAdded:Connect(CharacterAdded)
This code is a simple character-added method, in which upon the character is created, a new connection is set to the .Running event within the Humanoid. If the connection is already defined, it will print whether it is connected or not.
- Run the game, and reset your character.
- See unintended behavior: Connection stills says it is connected, despite another player being spawned in.
This is just the replication steps on the client, but you can also paste the first code sample in a ServerScript to see the difference from the Client and Server.