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

Need help making players change team after a timer hits 0?

Asked by 3 years ago

Hello I need some help with coding a team changer after a timer hits 0. I have the code for the timer, but I need some help with the team changing part. Here's the code:

local timer = 3
local hint = Instance.new("Hint", workspace)

for i = timer, 0, -1 do
    hint.Text = "You have: "..i.. " seconds left!"
    wait(1)
end

hint.Text = "The game has ended!"

for i,v in pairs(game.Players:GetPlayers()) do
    v.Character.Humanoid.Health = 0
    v.Players.TeamColor = BrickColor.new("Really red")
end

Its a very small script, but pretty much the timer is 3 seconds, and after the game ends it runs the ending prompt, which successfully kills the humanoid, but it does not change the players team. Any ideas?

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

You are most likely getting Players is not a valid member of Player "Players.kirdasmooth" error, this means that Players is not a property nor a child or v which is true. Roblox documentation on TeamColor says that TeamColor is property of Player, in your case v is the player, this means your solution is the following:

local timer = 3
local hint = Instance.new("Hint", workspace)

for i = timer, 0, -1 do
    hint.Text = "You have: "..i.. " seconds left!"
    wait(1)
end

hint.Text = "The game has ended!"

for i,v in pairs(game.Players:GetPlayers()) do
    v.Character.Humanoid.Health = 0
    v.TeamColor = BrickColor.new("Really red")
end

Even if it works, there are issues with this code... if at least one player does not have a character or humanoid at the time it loops, your whole loop will stop since you will try to set Health of non-existing humanoid to 0, you can fix that by checking if it actually exists:

local timer = 3
local hint = Instance.new("Hint", workspace)

for i = timer, 0, -1 do
  hint.Text = "You have: "..i.. " seconds left!"

  -- prefer using task.wait over wait, it's a newer version that has
  -- FAR better performance
  task.wait(1)
end

hint.Text = "The game has ended!"

for i,v in ipairs(game.Players:GetPlayers()) do
  local character = v.Character
  local humanoid

  if character then
    humanoid = character:FindFirstChildOfClass("Humanoid")
  end

  -- and humanoid.Health > 0 is not necessary but I find it sweat to not
  -- change value to 0 if it is already 0
  if humanoid and humanoid.Health > 0 then
    humanoid.Health = 0
  end

  v.TeamColor = BrickColor.new("Really red")
end

Notice I used ipairs, it is similar to pairs but is used to loop through arrays, it is faster and goes by order, it won't change your script in this case but is a good practice, here is an example of how array looks:

-- Elements go by order and all have numeric indexes starting from 1
local array = {
  [1] = 5,          -- 1
  [2] = 10,         -- 2
  [3] = workspace,  -- 3
  [4] = 200,        -- 4 ipairs will stop here since element [5] does not exist
  [6] = 340     -- ignored
}

for i, v in ipairs(array)
  print(i, v)
do
--> 1 5
--> 2 10
--> 3 Workspace
--> 4 200

-- GetChildren returns array in the following form so ipairs fits
local children = {
  [1] = Instance,
  [2] = Instance,
  [3] = Instance
}
0
Thank you a lot, been stuck on this for quite some time lol. I'm very new to this sorta stuff but I understood everything you talked about. Thanks for the answer. AROBLOXUSER12344 24 — 3y
Ad

Answer this question