SeverScript won't work after death?

My ServerScript won’t work after death.

local Players = game:GetService("Players")
	 
	Players.PlayerAdded:Connect(function(player)
    		wait(2)
	  local char = player.Character
	    if char then
	        local childs = char:GetChildren()
        for i,v in pairs(childs) do
	         if v:isA("Accessory") then
	       v:Destroy()
         end
	  end
   end
end)

How do I get it to work after the player dies?

The script is supposed to remove the players accessories, but after the player dies they get their accessories back.

probably should wait for the character to fully respawn

For example:

Your script only works when the player initially joins the game

local Players = game:GetService("Players")
	 -- you should use a function because they're reusable and help condense code
    local function RemoveAccessories(char)
       local childs = char:GetChildren()
        for i,v in pairs(childs) do
	         if v:isA("Accessory") then
	       v:Destroy()
         end
    end

Players.PlayerAdded:Connect(function(player)
    		wait(2)
	  if player.Character then -- check if the player's character has already loaded
          RemoveAccessories(player.Character)
      end

      player.CharacterAdded:Connect(RemoveAccessories) -- call the function when the player respawns
end)

what does this red line indicate?

image_2023-02-20_190702715

Place another end at the end of the function for RemoveAccessories; it’s just a syntax error

still dosent work, what migth I be missing?(I am mediocre at scripting, sorry)

local Players = game:GetService("Players")
	 
    local function RemoveAccessories(char)
       print("removing character accessories") -- just something to let you know if the function is running
       local childs = char:GetChildren()
        for i,v in pairs(childs) do
	         if v:isA("Accessory") then
	       v:Destroy()
         end
       end
    end -- added the extra 'end' here

Players.PlayerAdded:Connect(function(player)
    		wait(2)
	  if player.Character then
          RemoveAccessories(player.Character)
      end

      player.CharacterAdded:Connect(RemoveAccessories)
end)

If the print isn’t working at all, it’s likely that Players.PlayerAdded isn’t firing. In which case, you should make a loop that goes through each player in the game and calls a function for each of them

local function OnPlayerAdded(player)
    if player.Character then
          RemoveAccessories(player.Character)
      end

    player.CharacterAdded:Connect(RemoveAccessories)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

for _, plr in Players:GetPlayers() do
   OnPlayerAdded(plr) -- call the function for each player 
end

You just need to add CharacterAdded

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		task.wait(2)
		local childs = character:GetChildren()
		for i = 1,#childs do
			if childs[i]:isA("Accessory") then
				childs[i]:Destroy()
			end
		end
	end)	
end)
1 Like

this didn’t work. (sorry)

how world I be able to reference a player that is a child of workspace so then I could destroy the accessories that are on the player in workspace

It’s working for me and I’m not sure what you mean? “character” is already referencing the player’s character that’s in workspace

for _, part in pairs(character:GetChildren()) do
	if part:IsA("Accessory") then
		part.Handle:Destroy()
	end
end

oh, sorry. Mines not working. Btw, my script is located in a folder in workspace, I don’t know if that changes anything but I like to keep things organized. Other than that idk why it wouldn’t work

Preferable you would want to keep scripts inside of server
script service because then people cannot see the scripts…
Just because of security reasons and other stuff.

Pretty sure this works, if you haven’t got a script working then make a new server script and place it inside of server script service, then lastly copy and paste this code inside:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		for _, part in pairs(character:GetChildren()) do
			if part:IsA("Accessory") then
				part:Destroy()
			end
		end
	end)
	player.CharacterAdded:Connect(function(character)
		for _, part in pairs(character:GetChildren()) do
			if part:IsA("Accessory") then
				part:Destroy()
			end
		end
	end)
end)

Not the best way of making it, but works well.

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