Character Customization Error

Hello, I’ve been working on my clothing aspect for my CharacterCustomization UI for a bit. I want to make it so whenever I change the clothing via the Left and Right buttons on the ClonedCharacter (which is a dummy of the player’s character) it’ll then change the Player’s Character.

It works until you you begin pressing the UI a few times to change the ClonedCharacter clothing and then it’ll just not change the Player’s clothing anymore.

Here’s an example for one of the functions for it’s the same for all four functions I have

RightShirtButton.MouseButton1Click:Connect(function()
	if ClonedCharacter ~= nil then
		local FoundShirt = ClonedCharacter:FindFirstChild("Shirt")
		if FoundShirt~= nil then
			FoundShirt.ShirtTemplate = Shirts[1].ShirtTemplate
			PlayerUI.ShirtsFrame.Current.Value = 1
		end
	end
	Services.ReplicatedStorage.Events.UpdateShirt:FireServer(ClonedCharacter.Shirt.ShirtTemplate)
	PlayerUI.ShirtsFrame.Current.Value = PlayerUI.ShirtsFrame.Current.Value + 1
	if PlayerUI.ShirtsFrame.Current.Value > 1 then
		PlayerUI.ShirtsFrame.Current.Value = #Shirts
	end
	ClonedCharacter:WaitForChild("Shirt").ShirtTemplate = Shirts[PlayerUI.ShirtsFrame.Current.Value].ShirtTemplate
	if not ClonedCharacter:FindFirstChild("Shirt") then
		Instance.new("Shirt", ClonedCharacter)
	end
end)

So when I change the shirt or pants on the menu it doesn’t change the Player’s character’s shirt.


Only error I get is Attemp to index nil with ShirtTemplate or PantsTemplate for which ever button I press

And this is the script I have in SeverScriptService:

--clothing

game.ReplicatedStorage.Events.UpdateShirt.OnServerEvent:Connect(function(player, shirtTemplate)
	local Character = player.Character or player.CharacterAdded:wait()
	if shirtTemplate ~= nil then
		Character.Shirt.ShirtTemplate = shirtTemplate
	end
end)
game.ReplicatedStorage.Events.UpdatePants.OnServerEvent:Connect(function(player, pantsTemplate)
	local Character = player.Character or player.CharacterAdded:wait()
	if pantsTemplate ~= nil then
		Character.Pants.PantsTemplate = pantsTemplate
	end
end)

Any help will be well-appreated, thanks in advance!

2 Likes

Would you mind showing the line that got the error

2 Likes

Sure thing!

ClonedCharacter:WaitForChild("Shirt").ShirtTemplate = Shirts[PlayerUI.ShirtsFrame.Current.Value].ShirtTemplate```
1 Like

Can you try this

print(ClonedCharacter:WaitForChild("Shirt"))

2 Likes

Replace the whole line with it?

No… try to print it and see what happen

Oh, I’m sorry, I’m a little dumb with scripts. :sweat_smile:

I just did and here is what was in the Outut:

It says attempt to index nil with shirt template and not the shirt. Maybe try check your spelling on the shirt template.

Doesn’t seem to be the issue for it simply calls the ShirtTemplate property. I was reviewing the output and the error continues to happen on the same line, which is in the function for the LeftShirtButton

So here’s all the functions relating to the ClonedCharacter's shirt I hope that helps in figuring out this problem.

--Clothing
local Shirts = Services.ReplicatedStorage:WaitForChild("CustomClothing"):WaitForChild("Shirts"):GetChildren()

Shirt.MouseButton1Click:Connect(function()
	SkinTone:TweenPosition(UDim2.new(1.509, 0,0.759, 0), "Out", "Sine", 1, false)
	ShirtFrame:TweenPosition(UDim2.new(0.741, 0,0.079, 0), "Out", "Sine", 1, false)
	PantFrame:TweenPosition(UDim2.new(1.741, 0,0.177, 0), "Out", "Sine", 1, false)
end)

RightShirtButton.MouseButton1Click:Connect(function()
	if ClonedCharacter ~= nil then
		local FoundShirt = ClonedCharacter:FindFirstChild("Shirt")
		if FoundShirt~= nil then
			FoundShirt.ShirtTemplate = Shirts[1].ShirtTemplate
			PlayerUI.ShirtsFrame.Current.Value = 1
		end
	end
	Services.ReplicatedStorage.Events.UpdateShirt:FireServer(ClonedCharacter.Shirt.ShirtTemplate)
	PlayerUI.ShirtsFrame.Current.Value = PlayerUI.ShirtsFrame.Current.Value + 1
	if PlayerUI.ShirtsFrame.Current.Value > 1 then
		PlayerUI.ShirtsFrame.Current.Value = #Shirts
	end
	ClonedCharacter:WaitForChild("Shirt").ShirtTemplate = Shirts[PlayerUI.ShirtsFrame.Current.Value].ShirtTemplate
	print(ClonedCharacter:WaitForChild("Shirt"))
	if not ClonedCharacter:FindFirstChild("Shirt") then
		Instance.new("Shirt", ClonedCharacter)
	end
end)

LeftShirtButton.MouseButton1Click:Connect(function()
	if ClonedCharacter ~= nil then
		local FoundShirt = ClonedCharacter:FindFirstChild("Shirt")
		if FoundShirt~= nil then
			FoundShirt.ShirtTemplate = Shirts[1].ShirtTemplate
			PlayerUI.ShirtsFrame.Current.Value = 1
		end
	end
	Services.ReplicatedStorage.Events.UpdateShirt:FireServer(ClonedCharacter.Shirt.ShirtTemplate)
	PlayerUI.ShirtsFrame.Current.Value = PlayerUI.ShirtsFrame.Current.Value - 1
	if PlayerUI.ShirtsFrame.Current.Value > 1 then
		PlayerUI.ShirtsFrame.Current.Value = #Shirts
	end
	ClonedCharacter:WaitForChild("Shirt").ShirtTemplate = Shirts[PlayerUI.ShirtsFrame.Current.Value].ShirtTemplate
	print(ClonedCharacter:WaitForChild("Shirt"))
	if not ClonedCharacter:FindFirstChild("Shirt") then
		Instance.new("Shirt", ClonedCharacter)
	end
end)

GetChildren returns an array, not a dictionary (unless the below is a number vvv).

Can you try printing this out?

GetChildren is used cause Shirts is a folder in ReplicatedStorage which holds the shirts I’m using.
image

When running the game the output says 2, but only when I click the RightButton and not the Left

Definitely unexpected behavior. Could you try printing out Shirts[PlayerUI.ShirtsFrame.Current.Value] and Shirts’ contents? Maybe you’re using a StringValue, which of course uses a string and not a number (which causes this behavior).

Current is an IntValue, would a NumberValue work better?

Just checked and it still causes the same error:

Just for reference, the problem is that Shirts[PlayerUI.ShirtsFrame.Current.Value] is nil. This means the table “Shirts” doesn’t have a value that corrosponds to PlayerUI.ShirtsFrame.Current.Value.

I suspect the problem is from this section:

PlayerUI.ShirtsFrame.Current.Value = PlayerUI.ShirtsFrame.Current.Value - 1
if PlayerUI.ShirtsFrame.Current.Value > 1 then
	PlayerUI.ShirtsFrame.Current.Value = #Shirts
end

You reduce the index by one, but then only check if the index is too large. Instead, I think that the < operator should be used instead of the > operator.

This does get rid of the errors, but the shirts themselves no longer change on the ClonedCharacter no matter which button I click.
image

Even the player’s Character’s shirt changes with the opposite shirt.


image

2 Likes

Did some more tampering this morning and was able to fix it.

It came down to that being the problem. Thank you!

1 Like