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!"
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
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:
local array = {
[1] = 5,
[2] = 10,
[3] = workspace,
[4] = 200,
[6] = 340
}
for i, v in ipairs(array)
print(i, v)
do
local children = {
[1] = Instance,
[2] = Instance,
[3] = Instance
}
1 comment
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.