.CharacterAdded doesn't run

I’m trying to insert the player’s character into a folder inside workspace upon the character being added, and remove the character upon the humanoid dying, but for some reason the CharacterAdded function doesn’t run at all

this is a server script located inside of StarterCharacterScripts

local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)
local char

local function RemoveFromFolder()
	if workspace.Players:FindFirstChild(char.Name) then
		workspace.Players[char.Name]:Destroy()
	end
end

plr.CharacterAdded:Connect(function(model) -- nothing in here will run, not even a print 
	model.Parent = workspace.Players
	char = model
	
	local humanoid = char:WaitForChild("Humanoid")
	humanoid.Died:Connect(RemoveFromFolder)
end)
1 Like

Put your script in the server script service or workspace. When you put the script in startercharacter, the script is being replicated to the player’s character, a client structure, which is why the script won’t work

1 Like

The character(script.Parent) already exists before CharacterAdded event got created

2 Likes

did this

turns out roblox would instantly parent the character back to workspace upon me parenting it to the folder

not sure if this was the most optimal thing to do, but i just used runservice to change the parent

local plr
local char
local rService = game:GetService("RunService")

local function RemoveFromFolder()
	if workspace.Players:FindFirstChild(char.Name) then
		workspace.Players[char.Name].Parent = workspace
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	plr = player
	
	plr.CharacterAdded:Connect(function(model)
		rService.Heartbeat:Wait()
		model.Parent = workspace.Players
		char = model
		print(char.Parent)

		local humanoid = char:WaitForChild("Humanoid")
		humanoid.Died:Connect(RemoveFromFolder)
	end)
end)
1 Like

Reason is because the script is located under StarterCharacter

So due to it,
The script will only be cloned when the character spawns.
And when it dies then the script also gets removed. (not instantly)

2 Likes

Try doing this -

local plr
local char
local rService = game:GetService("RunService")

local function RemoveFromFolder()
	if workspace.Players:FindFirstChild(char.Name) then
		workspace.Players[char.Name].Parent = workspace
        char = nil
	end
end

local function AddToFolder(model)

        model.Parent = workspace.Players
        char = model

        local humanoid = char:WaitForChild("Humanoid")
		humanoid.Died:Connect(RemoveFromFolder)

end

game:GetService("Players").PlayerAdded:Connect(function(player)
	plr = player
	
        if plr.Character then 
          AddToFolder(plr.Character)
         end

	plr.CharacterAdded:Connect(function(model)
            rService.Heartbeat:Wait()
		AddToFolder(model)
		model.Parent = workspace.Players
	end)
end)

Note - being less OOP is not a good way of coding, I followed your style for the script above.

3 Likes

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