Character editor buttons not working after 3-4 clicks

I’m making a script of character editor, it works good on the firsts clicks, but after I click 3/4 times, it start to don’t work and in output appear errors about the :Clone() or/and :Destroy()

Script (local)

script.Parent.MouseButton1Click:Connect(function()
	local cloth = game.Players.LocalPlayer.CharacterValues.Shirt.Value
	
	if cloth == 12 then
		cloth = 1
		game.ReplicatedStorage.Character.Shirts:FindFirstChild("Shirt" .. cloth):Clone().Parent = workspace.Lobby.LobbyModel.LobbyCharacter
		workspace.Lobby.LobbyModel.LobbyCharacter:FindFirstChild("Shirt12"):Destroy()
	else
		cloth = cloth + 1
		game.ReplicatedStorage.Character.Shirts:FindFirstChild("Shirt" .. cloth):Clone().Parent = workspace.Lobby.LobbyModel.LobbyCharacter
		workspace.Lobby.LobbyModel.LobbyCharacter:FindFirstChild("Shirt" .. cloth - 1):Destroy()
	end
end)

Thanks for the help

If the shirt isn’t in there then its going to give an error. You can either use an if statement or a pcall function to avoid this. :slight_smile:

But you know why isn’t detecting?
Because I see on explorer (all on client) and the shirts are in the correct place.

No, not really since I don’t have any other info about what you are trying to do.

So, now it don’t give the error, but isn’t finding the shirt, so isn’t working…

Are you using pcall or an if statement?

if statement (I don’t know how to use pcall)

but for that I see, the problem is that the script isn’t finding the shirt, but the shirt is in the right location.

pcall(function()
game.ReplicatedStorage.Character.Shirts:FindFirstChild("Shirt" .. cloth):Clone().Parent = workspace.Lobby.LobbyModel.LobbyCharacter
		workspace.Lobby.LobbyModel.LobbyCharacter:FindFirstChild("Shirt12"):Destroy()
end)

But why using that if the problem is that the script can’t see the shirt?

(I don’t understand very much about pcall, so I don’t know the difference)

Thats a great model, but I’m trying to solve my script and not switch it for a free model that isn’t mine.

Ok, Well Good Luck On Fixing This!

It looks like you forgot to update the Shirt value on your character values. You should try using that to update the shirt instead of a local variable (which is going to get deleted when the function finishes anyways).

local Shirts = game:GetService("ReplicatedStorage").Character.Shirts
local LobbyCharacter = game:GetService("Workspace"):WaitForChild("Lobby").LobbyModel.LobbyCharacter

local cloth = game:GetService("Players").LocalPlayer.CharacterValues.Shirt

script.Parent.MouseButton1Click:Connect(function()
	local oldValue = cloth.Value
	
	if cloth.Value == 12 then
		cloth.Value = 1
		Shirts:FindFirstChild("Shirt1"):Clone().Parent = LobbyCharacter
		LobbyCharacter:FindFirstChild("Shirt12"):Destroy()
	else
		cloth.Value = cloth.Value + 1
		Shirts:FindFirstChild("Shirt" .. cloth.Value):Clone().Parent = LobbyCharacter
		LobbyCharacter:FindFirstChild("Shirt" .. oldValue):Destroy()
	end
end)
Shortened
local Shirts = game:GetService("ReplicatedStorage").Character.Shirts
local LobbyCharacter = game:GetService("Workspace"):WaitForChild("Lobby").LobbyModel.LobbyCharacter

local cloth = game:GetService("Players").LocalPlayer.CharacterValues.Shirt

script.Parent.MouseButton1Click:Connect(function()
	local oldValue = cloth.Value
	cloth.Value = (cloth.Value % 12) + 1
	
	Shirts["Shirt" .. cloth.Value]:Clone().Parent = LobbyCharacter
	LobbyCharacter["Shirt" .. oldValue]:Destroy()
end)
Better Organized
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Shirts = ReplicatedStorage.Character.Shirts

local Workspace = game:GetService("Workspace")
local lobby = Workspace:WaitForChild("Lobby")
local LobbyCharacter = lobby.LobbyModel.LobbyCharacter

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cloth = player.CharacterValues.Shirt

-- assuming every shirt in Shirts is named:
-- Shirt1, Shirt2, ..., Shirt12
local allShirts = {}
for _, shirt in ipairs(Shirts:GetChildren()) do
	local index = tonumber(string.match("Shirt%d", shirt.Name))
	allShirts[index] = shirt:Clone()
end

script.Parent.MouseButton1Click:Connect(function()
	local oldValue = cloth.Value
	cloth.Value = (cloth.Value % 12) + 1
	
	allShirts[cloth.Value].Parent = LobbyCharacter
	allShirts[oldValue].Parent = nil
end)

I haven’t tested any of these, so let me know if it errors!

3 Likes

The script worked fine, thank you.

1 Like