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)
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)
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)
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)