Script giving error

Dont know why but this script won’t work

i get this error: Workspace.WindyTundra.Character:28: attempt to index nil with ‘Destroy’

local char = script.Parent

local NewBC = game.ReplicatedStorage.New:Clone()
local head = char:FindFirstChild("Head")
local decal = head:FindFirstChild("face")
local BodyColors = char:FindFirstChild("Body Colors")

local shirt = Instance.new("Shirt", char)
shirt.ShirtTemplate = "rbxassetid://402312263"

game:GetService("RunService").RenderStepped:Connect(function ()

	NewBC.Parent = char

	decal.Texture = "rbxasset://textures/face.png"

	for i, part in pairs(char:GetDescendants()) do

		if part:isA("Accessory") then
			part:Destroy()
		end

		if part:isA("Pants") or part:isA("Shirt") and part.Name ~= "Clothing" then
			part:Destroy()
		end
	end
	
	BodyColors:Destroy()

end)

local shirt = Instance.new("Shirt", char)
shirt.ShirtTemplate = "rbxassetid://402312263"

The error Workspace.WindyTundra.Character:28: attempt to index nil with ‘Destroy’ means that it has not found an instance called “Body colors” inside the character.

In which case, you should add an if statement to check if the character contains any body colors or not, instead of resulting your script in an error.

why do you have this in RenderStepped?

local char = script.Parent

local NewBC = game.ReplicatedStorage.New:Clone()
local head = char:FindFirstChild("Head")
local decal = head:FindFirstChild("face")
local BodyColors = char:FindFirstChild("Body Colors")

local shirt = Instance.new("Shirt", char)
shirt.ShirtTemplate = "rbxassetid://402312263"

game:GetService("RunService").RenderStepped:Connect(function ()

	NewBC.Parent = char

	decal.Texture = "rbxasset://textures/face.png"

	for i, part in pairs(char:GetDescendants()) do

		if part:isA("Accessory") then
			part:Destroy()
		end

		if part:isA("Pants") or part:isA("Shirt") and part.Name ~= "Clothing" then
			part:Destroy()
		end
	end
	if BodyColors then
		BodyColors:Destroy()
	end

end)

local shirt = Instance.new("Shirt", char)
shirt.ShirtTemplate = "rbxassetid://402312263"

Essentially, this is checking if bodycolors is nil, which in this case it is because FindFirstChild hasn’t returned anything as it can’t be found. If it has been found, it will continue and destroy the instance.

You are not wrong, either.
A kind of function like this can most likely only be executed once, since it would have been completed the character’s adjustments and would not need a loop.

Did OP mistake it for…

game.Players.PlayerAdded:Connect(function(player: Player)
	player.CharacterAdded:Connect(function(character: Model)
		-- Character Code Here
	end)
end)

Perchance?

1 Like

that works, i just moved it to server script, i was using the RenderStepped Event because sometimes it wont work the first time around.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.