Script not repeated if player die

Hello there!

I made a script that says when a player joins in my game and his rank in the group is 1 then he will join a specific team he will get a specific item and their accessories will be removed and replaced with a specific hat.However, This can not be repeated when the player dies.

Can you help?

This is my script:

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local Teams = game:GetService("Teams")  

local function playerAdded(player) 
	
	local playerRank = player:GetRankInGroup(33119741)  
	print(player.Name .. "'s rank:", playerRank)  

	-- Set the player's team based on their rank
	if playerRank >= 0 then  
		player.Team = Teams:FindFirstChild("Civilian")
	end
		 
	local hat = game.ReplicatedStorage.Accessory
     local Humanoid = player.Character:FindFirstChild("Humanoid")
	local Bp = player:FindFirstChild("Backpack")  
	if playerRank <= 1 then  
		player.Team = Teams:FindFirstChild("Presidential Guard")  
		
		local clone = ReplicatedStorage.Musket:Clone()  
		clone.Parent = Bp  
		wait(1)
		Humanoid:RemoveAccessories()
		wait(1)
		hat:Clone().Parent = player.Character
	end

	
	if playerRank >= 2  then  
		player.Team = Teams:FindFirstChild("Member Of The Greeks Parliament")  
	end

	-- Assign a team based on the player's rank
	if playerRank >= 3 then 
		player.Team = Teams:FindFirstChild("Chiefs Of The Greek Presintential Guard")  
	end

	
	if playerRank >= 255 then 
		player.Team = Teams:FindFirstChild("Prime Minister") 
	end
end


players.PlayerAdded:Connect(playerAdded)
1 Like

Hey! Try this:


local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local Teams = game:GetService("Teams")  

local function playerAdded(player) 
    player.CharacterAdded:Connect(function(character)
        local playerRank = player:GetRankInGroup(33119741)  
        print(player.Name .. "'s rank:", playerRank)  

        -- Set the player's team based on their rank
        if playerRank >= 0 then  
            player.Team = Teams:FindFirstChild("Civilian")
        end

        local hat = game.ReplicatedStorage.Accessory
        local Humanoid = player.Character:FindFirstChild("Humanoid")
        local Bp = player:FindFirstChild("Backpack")  
        if playerRank <= 1 then  
            player.Team = Teams:FindFirstChild("Presidential Guard")  

            local clone = ReplicatedStorage.Musket:Clone()  
            clone.Parent = Bp  
            wait(1)
            Humanoid:RemoveAccessories()
            wait(1)
            hat:Clone().Parent = player.Character
        end


        if playerRank >= 2  then  
            player.Team = Teams:FindFirstChild("Member Of The Greeks Parliament")  
        end

        -- Assign a team based on the player's rank
        if playerRank >= 3 then 
            player.Team = Teams:FindFirstChild("Chiefs Of The Greek Presintential Guard")  
        end


        if playerRank >= 255 then 
            player.Team = Teams:FindFirstChild("Prime Minister") 
        end
    end)
end


players.PlayerAdded:Connect(playerAdded)

you have to add CharacterAdded, this fires every time player character is loaded (when player dies or resets as well)

There’s already a function which gets deleted if I put this.

What do you mean? Explain a bit more

I mean that if I add this function the other function I added will not work and it gets an issue.

Well you have to use characteradded in this case, so I’d suggest to change your second function

PlayerAdded event fires only If the player join to the server (Always, only first time). That’s why it’s doesn’t work anymore If player dies.

You should use CharacterAdded in all cases If you want to replicate objects to the Character or manage things every time the player model appear.

By the way, If you already use this event on other scripts, You should make it “globally”, make local functions and then execute the functions that you want to run; For example, convert the actual function to a module, and then call it every time CharacterAdded event fire.

game.Players.PlayerAdded:Connect(function(Player) >Player.CharacterAdded:Connect(function(Character)
DisplayPlayerTeam() — Example function
TeleportPlayer() — Example function
ChangeGUI() — Example function
end)
end)

Can you make it full script because I kinda blocked?

Here’s an example of how it should work (just to help you as @Exrexy already had the idea, and recommended in a server script inside of ServerScriptService):

game.Players.PlayerAdded:Connect(function(Player) -- When the player is added
	Player.CharacterAdded:Connect(function(Character) -- When the character is added
		-- do the stuff you wanted here
	end)
end)