Script that forces a certain walk/run animation if user is a certain rank in group

I have been trying to work on a script that will force a user to have a certain walk/run animation if they are a certain rank in a group.

Here’s the code I’ve created:

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function()
			if plr:GetRankInGroup(5030945) >= 13 then
				local humanoid = character:WaitForChild("Humanoid")
				
				for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
					playingTracks:Stop(0)
				end
				
				local animateScript = character:WaitForChild("Animate")
				animateScript.run.RunAnim.AnimationId = "rbxassetid://6300166393"        -- Run
				animateScript.walk.WalkAnim.AnimationId = "rbxassetid://6300166393"      -- Walk
			else
				print("User is not required rank")
			end
		end)
	end)
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

There are no errors in the output section, nor does it print “User is not required rank.” However, it doesn’t give the animation to me.

Any help would be appreciated, thank you!

Are you the owner of the group? I don’t think the rank of an owner is rank 13

I’m the co-owner of the group.

But that shouldn’t matter, because I’m asking the script to give the animation to users who are permission level 13 or higher.

All ranks above 13 get it too, so it can’t be.

From what I can see it seems like these animations are owned by you, not the group. Is this game owned by the group or you?

https://www.roblox.com/library/6300166393/Walking-Animation-R15

This game is owned by me. But I’m pretty sure that shouldn’t affect the script in any way.

Is this your entire script? Keep in mind functions don’t run on their own

This is my entire script, yes.

Originally, I was using this script where it gave the animation to everybody, and it was working just fine:

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")

	for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end

	local animateScript = character:WaitForChild("Animate")
	animateScript.run.RunAnim.AnimationId = "rbxassetid://6300166393"        -- Run
	animateScript.walk.WalkAnim.AnimationId = "rbxassetid://6300166393"      -- Walk
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

When I edited it to work with certain group ranks, then it stopped working.

why are you firing character added then player added first of all

you should be doing character added then getting the player name and searching for the player in players instead or just doing it on player added after character added

@S0MBRX was what I was going to say, I noticed you tried waiting for the character to load before the player did

local function onCharacterAdded(character)
    plr.CharacterAdded:Connect(function()
		if plr:GetRankInGroup(5030945) >= 13 then
			local humanoid = character:WaitForChild("Humanoid")
				
			for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
				playingTracks:Stop(0)
			end
				
			local animateScript = character:WaitForChild("Animate")
			animateScript.run.RunAnim.AnimationId = "rbxassetid://6300166393"        -- Run
			animateScript.walk.WalkAnim.AnimationId = "rbxassetid://6300166393"      -- Walk
		else
			print("User is not required rank")
		end
	end)
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Can’t believe that went over my head, let me fix that.

So this is the correct way to order it, right?

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		if plr:GetRankInGroup(5030945) >= 13 then
			local function onCharacterAdded(character)

(This isn’t the whole script, of course, just the beginning)

Remove this, you’re already calling the CharacterAdded event before that

Players.PlayerAdded:Connect(function(plr)
    if plr:GetRankInGroup(5030945) >= 13 then
	    plr.CharacterAdded:Connect(function(character)

One final question and then I promise I’ll leave you alone lol.

What exactly am I going to do with these:

local function onPlayerAdded(player)
    player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Because if onCharacterAdded isn’t a function then onPlayerAdded won’t work. Should I replace onCharacterAdded with simply “character?”

Your functions were kind of unnecessary, so I’ve changed your script a little. Let me know if it works

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(player)
    if player:GetRankInGroup(5030945) >= 13 then
        player.CharacterAdded:Connect(function(character)
			local humanoid = character:WaitForChild("Humanoid")
				
			for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
				playingTracks:Stop(0)
			end
				
			local animateScript = character:WaitForChild("Animate")
			animateScript.run.RunAnim.AnimationId = "rbxassetid://6300166393"        -- Run
			animateScript.walk.WalkAnim.AnimationId = "rbxassetid://6300166393"      -- Walk
    	end)
	else
		print("User is not required rank")
	end
end

You don’t even need this on a server script. You can change the animationid’s locally and the local animate script will read them just fine.

Yep, that would work as well :+1: