Help Needed: How to make a script which changes a players team after death?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Basically, I’m making a game with two teams. For this example let’s have Team 1 and Team 2. I want a script where; when a player on Team 2 dies from a player from Team 1, the dead Team 2 player respawns on Team 1. But, I do not want it to work the other way around… Like if a Team 2 player kills a Team 1 Player, I don’t want that to affect the Team 1’s team, so they’ll still respawn as Team 1, as they were originally.

  1. What is the issue? Include screenshots / videos if possible!

Well, to me, this seems like a complicated script, and I’m not quite sure where to start.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I did surf the developer hub and google, and couldn’t quite find any similar scripts which tend to what I’m trying to achieve.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I don’t want player reset’s or just death from like falling off map etc, to have any affect. I only want Team 2 players to switch to Team 1 if a Team 1 player kills them.

1 Like

It would make sense to start by creating a form of Tagging, where if a Player is hit by a weapon of some sorts, you can apply a Value saying a Player hit them, One Example is the Classic Sword Pack, where if a Player hits another Player with a Sword, it applys an ObjectValue called creator, with its Value being the Player that hit them, and then when they died, the game would check their Humanoid (Where the ObjectValues were Placed), and give points to the Player that hit them, During Gameplay they will not be there forever, they will be deleted overtime using Debris, which allows you to add an object to be deleted at a Specified time,

This would basically be the script that is in the Sword Pack.

local Debris = game:GetService

local function TagPlayer(player, targetHumanoid)
    -- the player parameter is your player
    -- the targetHumanoid parameter is the player you attacked
    local creator = Instance.new("ObjectValue") -- Creates ObjectValue
    creator.Name = "creator" -- Applies the Name
    creator.Value = player -- the Value of the ObjectValue is you
    creator.Parent = targetHumanoid -- Applies this tag to the Target Player

    Debris:AddItem(creator, 3) -- will deleted in 3 Seconds
end

-- Pretty Simple
-- But there is one big issue with this
-- When checking if the Player was killed by something
-- it may pick another player whose tag was still on them at the time
-- so when applying tags, you need to make sure that they dont get the same 
-- amount as the person who hit them last, if anything at all.
-- so when applying tags, either remove them, or change a Property
-- to Give them some from of "assist"

-- Thats up to you however, deal with it as you wish

The Second Part is to Confirm who killed them, There is a Event within the Humanoid called Died, which if i guessed by the name (good job) fires when they Die, you can use that to check their Humanoid for these creator item, and their Value to retrieve the Player who killed them, from there you can check what team that are on, and if they are on Team 1, change the Player who died to Team 1, To Change Teams, you have to Manually change the Team Instance on their Player Instance of their Properties, called Team (assuming you dont have your own Team System).

You can check these out, these should contain some of the “Properties” or Services I was referring to:

This might work?

Localscript:

local event = [remoteevent]
game.Players.LocalPlayer.Character.Died:Connect(function() event:FireServer end)

Script

local event = [remoteevent]
event.OnServerEvent:Connect(function(player)
if player.Team == game.Teams.Team2 then
player.Team = game.Teams.Team1
end
end)
1 Like