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)
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!