Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Error: attempt to call method 'Destroy' (a nil value)?

game.Players.PlayerAdded:connect (function(players)
	 players.CharacterAdded:connect (function(character)
		--Waiting
		game.ServerStorage:WaitForChild("UniformCode")
		local plrfile = game.ServerStorage.UniformCode:WaitForChild(players.Name)
		local Codes = plrfile:WaitForChild("Codes")


		local Hats1 = Codes:WaitForChild("Hats1")
		local Hats2 = Codes:WaitForChild("Hats2")
		local Hats3 = Codes:WaitForChild("Hats3")
		local Shirts = Codes:WaitForChild("Shirts")
		local Pants = Codes:WaitForChild("Pants")
		local BodyColors = Codes:WaitForChild("BodyColors")
		local Express = Codes:WaitForChild("Expressions")
		
		--Assigning
		if BodyColors.Value == 0 then
			character:GetChildren("Body Colors"):Destroy()
		end
	end)
end)

Alright I have this script when you join the game it gives random outfit, well I'm working on a script that does so. Anyway my problem happens on line 19 when Im trying to remove all the things the player has(exp their bodycolor) can someone tell me how to remove BodyColors from the players model?

3 comments

0

GetChildren() does not get arguments passed to it, and will return a table of items. Instead try, character:FindFirstChild("Body Colors")

0

it should be character[Body Colors]:GetChildren():Destroy or character:FindFirstChild ("Body Colors"):Destroy ()

1

You can't destroy a table.....................

2 answers

When you getchildren, you're returning a read-only table of values.

:destroy() is also not a method of a table.

You should use a for loop and index each player and destroy the bodycolors from there.

if you assign the children of character to a variable, you can do this.

local BodyColors = character:GetChildren()

for i=1, #BodyColors do
	if BodyColors[i]:IsA("Body Color") then
		BodyColors[i]:Destroy()
	end
end

basically anything with a lowercase letter directly after a : like :destroy is deprecated. :Destroy() itself though is deprecated too, so :Remove() would work. For your purposes, though, you can't just remove the body colors. You have to set them to something like fossil grey or something like that.

2 comments

0

No. Workspace is deprecated (the global, not service), and now you have to use workspace. Wait() is deprecated, you need to use wait().

0

Also, :Remove was deprecated in favor of :destroy. Then, :destroy got deprecated for :Destroy.

This discussion is preserved as it was. New answers, comments, and votes are closed.