Can't apply Texture with Code to part

Hi all,

I’m trying to add textures to parts in workspace via code, but can’t get the texture to show up
My Code:

local allCreation = game.Workspace:GetDescendants()

for i,v in pairs(allCreation) do
print("testing "..v.Name)
	if v:IsA("Part") and  v.Name  ~= "BasePlate" or v:IsA("Basepart") and v.Name  ~= "BasePlate" then

			local topTexture ="Texture")
			topTexture.Texture =  "rbxassetid://9929386574"
			topTexture.Parent = v
			topTexture.Face = "Front"
  1. Texture is not moderated
  2. tried rearranging the elements some
  3. tried several things in place of “rbxassetid”

Help? Thanks? !

1 Like

There’s a few issues I found:

  • I think you’re using the marketplace id rather than the asset id. You can get the asset id by manually pasting the marketplace ID into the “Texture” property. It will automatically convert to the asset id, which you can then copy and use in your code. I’ve done this for you and swapped the ID out for the correct one below.
  • Baseplates are usually named with a lowercase p, so unless you renamed it, your code won’t catch the Baseplate exception
  • The conditional logic on line 5 can be simplified, I’ve cleaned it up a bit below
  • It’s good practice for performance purposes to set the parent of a new instance last, so that changes you make after parenting it do not have to fire events and replicate. (Once it’s in the data model, i.e. has a parent, then all changes start firing events)
A couple other coding style notes:
  • It’s good practice to use the full enum name rather than a string to avoid making any spelling errors and be explicit about what you’re setting the value to
  • I opt for ipairs when working with an array-style table (i.e. indexes are contiguous integers starting from 1), pairs for any other table, just so it’s clear to the reader what kind of table I’m expecting
  • It’s good practice to reference all services via :GetService() for consistency
  • I opt to use guards, i.e. a conditional that prevents the rest of the code from running if not met, rather than nesting things inside the conditional block. This prevents your code from getting indented too far, which can damage readability.
  • It’s good practice to use descriptive variable names, such as part or instance rather than v so that it’s easier to read at a glance. I use the _ as a garbage variable, i.e. a variable I’m not using but is required to exist due to the syntax of loops.
  • The end for a code block should always be indented at the same level as the beginning of the code block. In the case of your if block, the corresponding end was indented one too far.
local Workspace = game:GetService("Workspace")

local allCreation = Workspace:GetDescendants()

for _, part in ipairs(allCreation) do
	if part.Name == "Baseplate" or not part:IsA("BasePart") then

	local topTexture ="Texture")
	topTexture.Texture = "rbxassetid://9929386527"
	topTexture.Face = Enum.NormalId.Front
	topTexture.Parent = part

See if that works for you.

1 Like