PlayerAdded not working properly

I have a script in server script service that’s supposed to add a folder to someone’s character whenever they join, but whenever the player dies the script stops adding the folders to the character. How do I fix this?

The script is in ServerScriptService

local BodyVariables = script.BodyVariables
local CharacterVariables = script.CharacterVariables
local players = game:GetService("Players")



players.PlayerAdded:Connect(function(plr)
	local Character = plr.Character or plr.CharacterAdded:Wait()
	
	local VariableFolder1 = BodyVariables:Clone()
	VariableFolder1.Parent = Character

	local VariableFolder2 = CharacterVariables:Clone()
	VariableFolder2.Parent = Character
	
end)

You’ll wanna do something like this then:

players.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function(Character)
      --this triggers everytime a character is added to the player.
  end)
end)

With this you’ll clone your folder into the character every time a Character gets added.

3 Likes
local BodyVariables = script.BodyVariables
local CharacterVariables = script.CharacterVariables
local players = game:GetService("Players")


local function FolderReplace()
   	local Character = plr.Character or plr.CharacterAdded:Wait()
	
	local VariableFolder1 = BodyVariables:Clone()
	VariableFolder1.Parent = Character

	local VariableFolder2 = CharacterVariables:Clone()
	VariableFolder2.Parent = Character
end


players.PlayerAdded:Connect(function(plr)
   FolderReplace()
end)


for a, b in ipairs(players:GetPlayers()) do
  b.CharacterAdded.Connect(function()
  --players character got added or respawned
   FolderReplace()
  end)
end

you do not need this function try this


game.Players.PlayerAdded:Connect(function(plr)
1 Like

You have a timing problem. When the player is added it doesn’t necessarily mean their character is already loaded.

players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
      --Code in here
  end)
end)
1 Like