How to make all players of a specific team dance?

Hello everyone!

I’m trying to write a function that makes all players of a particular team dance, but I cant seem to get it to work. Here my code so far:

local animation = game.workspace.R6Dance
function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		local humanoid = player:WaitForChild("Humanoid")
		local dance = humanoid:LoadAnimation(animation)
		dance:Play()
        wait(5)
        dance:Stop()
	end
end

Has anyone been able to make this work? Thanks in advance!

~innit_2winnit

2 Likes

Hello innit_2winnit :wave:

Are you getting any errors when calling the “allplayersdance” function?


This is the console when the function is called. The red mark is the player’s name.

1 Like

Ahhh, I see the problem.
You are using the :WaitForChild("Humanoid") Method on a player-model.
Player-Models do not have the Humanoid.

Try:

local humanoid = player.Character:FindFirstChild("Humanoid")

Then perform a check to see if the player has a humanoid

if (humanoid) then
  -- make the player dance
end

Sometimes a player can have no humanoid (when loading into the game for example)

I hope this helped you.

DosenSuppe2_0

All done!


Unfortunately, we have a new set of errors! :cold_sweat:

1 Like

Post your editted code here so we can see what you did

Oops! Forgot about that :sweat_smile:

Here:

function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		local humanoid = player:FindFirstChild("Humanoid")
		local dance = humanoid:LoadAnimation(animation)
		if (humanoid) then
			dance:Play()
			wait(10)
			dance:Stop()		
		else
			print("No humanoid! Error!")
		end
	end
end
1 Like

You’re still using the Player object to reference the humanoid. Do what @DosenSuppe2_0 said:


function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		local humanoid = player.Character:FindFirstChild("Humanoid")
	
		if humanoid then
	local dance = humanoid:LoadAnimation(animation)
			dance:Play()
			wait(10)
			dance:Stop()		
		else
			print("No humanoid! Error!")
		end
	end
end
1 Like

Thank you for your help! The players are now dancing!

One slight issue, only one of them dances, then after 10 seconds the next one dances. I’m trying to make them all dance simultaneously for 10 seconds. I’ve tried changing the script to this:

function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		local humanoid = player.Character:FindFirstChild("Humanoid")
	
		if humanoid then
	local dance = humanoid:LoadAnimation(animation)
			dance:Play()
		
		else
			print("No humanoid! Error!")
		end
	end
	wait(10)
	dance:Stop()
end

But to no avail. I’ve even tried creating a whole other function called allplayersstopdancing() but again, can’t get it to work. Do you have any idea on how I could achieve this?

1 Like

Try doing the following:

function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		task.spawn(function()
			local humanoid = player.Character:FindFirstChild("Humanoid")
	
			if humanoid then
				local dance = humanoid:LoadAnimation(animation)
				dance:Play()
			else
				print("No humanoid! Error!")
			end

			task.wait(10)
			dance:Stop()
		end)
	end
end
1 Like

It does stop the dance… for me, but the other player continues dancing despite the first player stopping!

Sidenote: The dance is meant to be the R6 /e dance2 animation, but for some reason the arms don’t fully rise like they’re meant to? Like they sit at a 45 degree angle as opposed to the 90 degree T-pose they’re meant to. Is that just a bug or am I doing something wrong?

1 Like

Try this:

function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		task.spawn(function()
			local humanoid = player.Character:FindFirstChild("Humanoid")
	
			if humanoid then
				local track = humanoid:LoadAnimation(animation)
				track:Play()

				task.wait(10)
				track:Stop()
			else
				print("No humanoid! Error!")
			end
		end)
	end
end
1 Like

It works perfectly! Thank you :slight_smile:
I will mark you answer as the solution.
Do you have any idea why the characters arms aren’t fully up like they’re supposed to?
image
If you don’t, I might make another post asking the devforum.
Thank you for your help regardless! :slight_smile:

~innit_2winnit

2 Likes

Try changing the track priority. Change the code to this and tell me if it works:

function allplayersdance()
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		task.spawn(function()
			local humanoid = player.Character:FindFirstChild("Humanoid")
	
			if humanoid then
				local track = humanoid:LoadAnimation(animation)
				track.Priority = Enum.AnimationPriority.Action
				track:Play()

				task.wait(10)
				track:Stop()
			else
				print("No humanoid! Error!")
			end
		end)
	end
end
1 Like

You are an absolute wizard!! It works flawlessly! Thank you for your help :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.