So I am making a script, but it had an error.
Script:
local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
if team.Name == "In-Game" and #players <= 1 then
local humanoid = players.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
break
end
end
end
I’m not quite sure about what team:GetPlayers().Character returns, but I think it’s a table, so not an object. Let me check first though, before I explain something that isn’t true.
if team:GetPlayers().Character[1] then
local humanoid = players.Character[1]:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
break
end
end
:GetPlayers returns a table of current players. If you want to reference the actual players you can just use a for loop and reference the character using Player.Character.
for Index, Player in ipairs(team:GetPlayers)do
local Character = Player.Character;
-- do whatever else
end;
So I just found out team:GetPlayers().Character returns nothing. You would have to use a for loop.
for _, player in ipairs(team:GetPlayers()) do --Go through every value in the table, ipairs is faster because it isn't a random order.--
local character = player.Character or player.CharacterAdded:Wait() --Get the character or wait for the character if it isn't there yet.--
local humanoid = character:FindFirstChildWhichIsA("Humanoid") --Check if there is a humanoid inside the character / reference the humanoid inside the character.--
if humanoid then --Check if there is a humanoid inside the character.--
humanoid.Health = 0 --Set the humanoid's health property to 0 so the character dies.--
break --Break the loop.--
end
end
Edit after 6 minutes:
I totally forgot you checked if it was only one person before, therefore your way is also great.
local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
if team.Name == "In-Game" and #players <= 1 then
local Character = players[1]
local humanoid = Character.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
break
end
end
end
That will only take the FIRST player in the table and kill him. Every other player will be unaffected. You can test this by going in studio and doing a 2 player play test.
This will still throw an error, potentially. You check if the size of players is less than or equal to 1. When it’s 0, this script will error as players[1] will be nil.
Assuming you want to kill all players in a single team, here’s what you’d do:
local team = game:GetService("Teams").InGame
local players = team:GetPlayers()
for _, player in ipairs(players) do
local character = player.Character
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
For just the first player:
local team = game:GetService("Teams").InGame
local player = team:GetPlayers()[1]
if player then
local character = player.Character
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid") -- this checks `character` also exists
if humanoid then
humanoid.Health = 0
end
end