I’m making a UI that plays an Animation to players, basically what I want to do is get the players from a team but first I need to find the specific team by name
Heres the script that I’m working on:
local teams = game:GetService("Teams"):GetTeams()
local teamName = script.Parent.Parent.TextBox2.Text
script.Parent.MouseButton1Click:Connect(function()
local id = script.Parent.Parent.TextBox.Text
script.Parent.Animation.AnimationId = id
wait(1)
for _, team in pairs(teams) do
if table.find(teamName, team.Name) then
local players = team:GetPlayers()
local char = players.Character or players.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(script.Parent.Animation)
animationTrack:Play()
end
end
end)
table.find() requires the first parameter to be a table and the second parameter to be an element (instance). It returns the index of the element if found, and you can just use that to find the respective team and its properties. Also GetPlayers() is a table, so you need to loop through it to reference the player. Oh and also Humanoid:LoadAnimation is deprecated, so you consider using Animator:LoadAnimation instead.
So in this case, you don’t need the for loop.
local teams = game:GetService("Teams")
local teamName = script.Parent.Parent.TextBox2.Text
script.Parent.TextButton.MouseButton1Click:Connect(function()
local id = script.Parent.Parent.TextBox.Text
script.Parent.Animation.AnimationId = id
wait(1)
local index = table.find(teams:GetTeams(), teams:FindFirstChild(teamName))
if index then
local team = teams:GetTeams()[index]
for _, plrs in pairs(team:GetPlayers()) do
local char = plrs.Character or plrs.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(script.Parent.Animation)
animationTrack:Play()
end
end
end)
You can simply index the team by its name using a simple variable.
local Teams = game:GetService("Teams")
local AnimIDTextBox = script.Parent.Parent.TextBox
local TeamNameTextBox = script.Parent.Parent.TextBox2
local Button = script.Parent
Button.MouseButton1Click:Connect(function()
local AnimID = AnimIDTextBox.Text
local TeamName = TeamNameTextBox.Text
local Team = Teams[TeamName] -- No need for table.find because you can simply index any object by name
for _, plrs in pairs(Team:GetPlayers()) do
local Char = plrs.Character or plrs.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(script.Parent.Animation)
animationTrack:Play()
end
end)