Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an On Death Team Changer?

Asked by 7 years ago

How would I write a script that would make people change teams after say about 5 deaths? I know the meadows has something similar to this but I do not know how to look at that, pleas help?

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Storing death counts

Well, every time the player's death is recorded, you can increment an integer saved on the server that represents how many times they've died. This integer could perhaps be saved as a key/value pair inside a table when the player joins. Here's an example:

-- (Server script)
-- Game services
local Players = game:GetService("Players")

-- Dictionary of players w/ recorded deaths
local playerDeaths = {}

-- When a player joins the game
local function playerJoined(player)
    playerDeaths[player] = 0 -- Associate their death count (0), with a key (being the player)
end

-- Make sure when they leave, this data is erased.
local function playerLeft(player)
    playerDeaths[player] = nil
end

-- Connect callback functions to events
Players.PlayerAdded:Connect(playerJoined)
Players.PlayerRemoving:Connect(playerLeft)

Now when a player joins, they'll be stored as an index in the table, associated with their death count integer.

Recording deaths

Recording the actual death could be done on the client, where we'll then notify the server of the event through a remote. We could do this several different ways, but the easiest is probably to use the Died event of Humanoid. We'll also assume a RemoteEvent called "DiedEvent" exists inside the ReplicatedStorage service.

-- (Local script)
-- Game services
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")

-- Remote event
local DiedEvent = RepStorage:WaitForChild("DiedEvent")

-- Client details
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local human = character:WaitForChild("Humanoid")

-- Execute when Died event is fired
local function playerDied()
    DiedEvent:FireServer()
end

-- Connect events to callbacks
human.Died:Connect(playerDied)

Now all we need to do is create the server-side event for the remote (called OnServerEvent), that the client can fire whenever the player has died.

-- (Back in server script)
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")

-- Remotes
local DiedEvent = RepStorage:WaitForChild("DiedEvent")

-- Dictionary of players w/ recorded deaths
local playerDeaths = {}

-- Set death count limit
local DEATH_COUNT_LIMIT = 5

-- When a player joins the game
local function playerJoined(player)
    playerDeaths[player] = 0 -- Associate their death count (0), with a key (being the player)
end

-- Make sure when they leave, this data is erased.
local function playerLeft(player)
    playerDeaths[player] = nil
end

-- Function executed when client fires 'DiedEvent'
local function onPlayerDeath(player)
    -- Increment death count
    local deathCount = playerDeaths[player] + 1
    playerDeaths[player] = deathCount

    -- Check if the death count is over limit
    if deathCount >= DEATH_COUNT_LIMIT then
        player.TeamColor = BrickColor.new(NEW_TEAM_COLOR) -- "NEW_TEAM_COLOR" is where you'll add what team you want the player to move to.
        playerDeaths[player] = 0 -- Reset death count
    end
end

-- Connect callback functions to events
Players.PlayerAdded:Connect(playerJoined)
Players.PlayerRemoving:Connect(playerLeft)
DiedEvent.OnServerEvent:Connect(onPlayerDeath) -- Let's not forget to connect the function to the event

So now whenever the player dies, the client will notify the server to update and check the player's death count, and change the player's team accordingly. This was a bit of information all at once, so if you don't understand most of it feel free to ask a question and I'll try to clarify.

EDIT

Also, if you'd like to make it so the players can switch between teams interchangeably after every 5 deaths without creating a special case for it, you could just make it so their new team is the opposite of their current team. Here's an example of how you could create that:

local function getOppositeTeamColor(player)
    local t1, t2 = unpack(TeamService:GetTeams()) -- Assuming 'TeamService' is defined
    t1, t2 = t1.TeamColor, t2.TeamColor
    return player.TeamColor == t1 and t2 or t1
end

This basically just says return t2 if player is t1, and return t1 if player is in t2. So a final product on the server-side would look something like this:

-- (Back in server script)
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")
local TeamService = game:GetService("Teams") -- Define TeamService

-- Remotes
local DiedEvent = RepStorage:WaitForChild("DiedEvent")

-- Dictionary of players w/ recorded deaths
local playerDeaths = {}

-- Set death count limit
local DEATH_COUNT_LIMIT = 5

-- Utility --
-- Return the opposing team color of given player
local function getOppositeTeamColor(player)
    local t1, t2 = unpack(TeamService:GetTeams())
    t1, t2 = t1.TeamColor, t2.TeamColor
    return player.TeamColor == t1 and t2 or t1
end

-- When a player joins the game
local function playerJoined(player)
    playerDeaths[player] = 0 -- Associate their death count (0), with a key (being the player)
end

-- Make sure when they leave, this data is erased.
local function playerLeft(player)
    playerDeaths[player] = nil
end

-- Function executed when client fires 'DiedEvent'
local function onPlayerDeath(player)
    -- Increment death count
    local deathCount = playerDeaths[player] + 1
    playerDeaths[player] = deathCount

    -- Check if the death count is over limit
    if deathCount >= DEATH_COUNT_LIMIT then
        player.TeamColor = getOppositeTeamColor(player)
        playerDeaths[player] = 0 -- Reset death count
    end
end

-- Connect callback functions to events
Players.PlayerAdded:Connect(playerJoined)
Players.PlayerRemoving:Connect(playerLeft)
DiedEvent.OnServerEvent:Connect(onPlayerDeath) -- Let's not forget to connect the function to the event

Just thought that'd be a nice bit of information to add, hope it helps!

Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Well, for starters, you would want to see how many deaths the player has. I assume you have a leaderboard (your reference to meadows makes me assume you're using this for a group), if not, this wiki page will teach you how to set one up.

In essence what you want to do, is make this script wait for the player's death to reach 5, and when that happens, change the team. For this script, I will assume that on the leaderboard the "deaths" section is labeled "WOs".

From there, you want to

game.Players.PlayerAdded:connect(function(plr) -- This is defining who our player is.
local WOs = plr.leaderstats.WOs.Value -- This is how many Deaths they have on the leaderboard
while true do
wait()
if WOs == 5 then -- When it see's that there are 5 deaths, it triggers the below code
Player.TeamColor = BrickColor.new("Put the color of the new team here") -- Changing the player's team
else 
end
end
end)



I am not the most efficient scripter, so maybe parts of this aren't as efficient as they could be, but if I were making a game then this is how I would do it. Comment with any questions!

Answer this question