Need help making a custom R6 rig for the gnomes in my game

So, I want to make the following - and I am kindly asking for your help: there are two teams in my game, and one of them should have a custom R6 rig which has smaller/shorter torso/arms/legs, and about similar sized head.

Here’s the code so far:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local GnomeTeam = Teams:FindFirstChild("Gnomes") -- Replace with your team's name

-- Function to change the player's character to the custom rig
local function ChangeToCustomRig(player)
	local customRig = game.ServerStorage:WaitForChild("CustomR6Rig"):Clone()
	customRig.Name = player.Name

	-- Wait for necessary parts to be loaded in the custom rig
	local humanoidRootPart = customRig:WaitForChild("HumanoidRootPart", 5)
	local torso = customRig:WaitForChild("Torso", 5)
	if not humanoidRootPart or not torso then
		warn("Custom rig is missing necessary parts")
		return
	end

	-- Set the CFrame of the HumanoidRootPart
	humanoidRootPart.CFrame = player.Character:WaitForChild("HumanoidRootPart").CFrame

	-- Copy clothes
	local character = player.Character
	for _, item in pairs(character:GetChildren()) do
		if item:IsA("Clothing") or item:IsA("Accessory") then
			local newItem = item:Clone()
			newItem.Parent = customRig
		end
	end

	player.Character = customRig
	customRig.Parent = workspace
	print("Custom rig applied for", player.Name)
end

-- Function to handle when a player's character is added
local function onCharacterAdded(player)
	if player.Team == GnomeTeam then
		ChangeToCustomRig(player)
	end
end

-- Function to handle team changes
local function onTeamChanged(player)
	if player.Team == GnomeTeam then
		if player.Character then
			ChangeToCustomRig(player)
		end
	else
		-- Revert to the default rig if they leave the team
		if player.Character then
			player:LoadCharacter()
		end
	end
end

-- Function to handle new players joining the game
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(player)
	end)

	player:GetPropertyChangedSignal("Team"):Connect(function()
		onTeamChanged(player)
	end)

	-- Initial check in case the player is already on the team when they join
	if player.Team == GnomeTeam and player.Character then
		ChangeToCustomRig(player)
	end
end

-- Connect the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

-- For existing players in the game
for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

Currently, I get the following errors (rig’s animate script is set to default, but what would I even change it to? I am fine with default animations, it’s just a slightly smaller version of regular R6. Huge added bonus is not having to animate separately for either team)

Please, help me guide in the right direction, or explain to me what needs to be done? You don’t have to show what you do with code, but that would be immensely appreciated because this seems a bit confusing. There is a possible infinite yield when I join the team I want the rig of to be applied to. Seems to work fine after, could be just due to rig taking a while to load, but maybe there is a better way to do this in general.

Thank you in advance

3 Likes

Maximum event re-entrancy is caused by something in your event calling itself repeatedly.

The only code I can see that I think might be causing this is the fact that when the onCharacterAdded function is called it ends up triggering it again as the ChangeToCustomRig adds a new character for the player.
Complete speculation, I don’t have access to studio right now but if it’s not that then look for anything that would call the functions mentioned in the error messages in a loop.

There’s likely a better explanation / solution for your problem

3 Likes

i didnt look at the op’s code at all but you’re probably right;

i have a similar issue of setting player.Character causing characteradded to fire recursively. probably going to have to use plr:LoadCharacter and handle everything manually

1 Like