Help with Textures

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a crate with textures on the front, left, right, and back face with a random texture, it’s hard to explain but here’s an image:

I want it so that the crate will spawn in with the same texture (I’m using math.random) on all sides, not including the top or bottom.

  1. What is the issue? Include screenshots / videos if possible!
    I’m not entirely sure on how I can do this, I tried using the in pairs function but it wouldn’t really work, and I’m not sure how I could get each texture to face a different direction.

Screenshot 2024-01-14 145213

Here is the code I’m using:

local ServerStorage = game:GetService("ServerStorage")
local Images = ServerStorage:WaitForChild("Textures"):GetChildren()

local Model = script.Parent

local Hitbox = script.Parent:WaitForChild("Hitbox")
local Crate = Model:WaitForChild("Parts"):FindFirstChild("Crate")

local Image = Images[math.random(1, #Images)]:Clone()

for i, v in pairs(Crate:GetChildren()) do
	if v.ClassName == "Texture" then
		Image.Parent = v.Texture
	end
end

local Sound = Hitbox:WaitForChild("Boom")

local ProximityPrompt = script.Parent.Hitbox.ProximityPromptAttachment:WaitForChild("ProximityPrompt")

function CreateExplosion()
	local Explosion = Instance.new("Explosion")
	Explosion.BlastRadius = 1000
	Explosion.BlastPressure = 350
	Explosion.Position = Model.PrimaryPart.Position
	Explosion.Parent = Model
	Sound:Play()
end

ProximityPrompt.Triggered:Connect(function(Player)
	CreateExplosion()

	ProximityPrompt.Enabled = false

	task.wait(2.5)

	Model:Destroy()
end)

I’d appreciate it if you’d help, thanks.

I guess you could get that texture and clone one and set to each side front , back, left, right.

To add on to what @aabols2010 has said, this is what your code is doing:

local ServerStorage = game:GetService("ServerStorage")
local Images = ServerStorage:WaitForChild("Textures"):GetChildren()

local Model = script.Parent

local Hitbox = script.Parent:WaitForChild("Hitbox")
local Crate = Model:WaitForChild("Parts"):FindFirstChild("Crate")

local Image = Images[math.random(1, #Images)]:Clone() -- Cloning once

for i, v in pairs(Crate:GetChildren()) do
	if v.ClassName == "Texture" then -- Finding already existing textures
		Image.Parent = v.Texture -- Moving the single cloned image to be a child of an existing texture
	end
end

If you already have 4 textures inside your crate, your code should be like this:

local ServerStorage = game:GetService("ServerStorage")
local Images = ServerStorage:WaitForChild("Textures"):GetChildren()

local Model = script.Parent

local Hitbox = script.Parent:WaitForChild("Hitbox")
local Crate = Model:WaitForChild("Parts"):FindFirstChild("Crate")

local Image = Images[math.random(1, #Images)].Texture -- Setting "Image" to the "Texture" property of a random texture

for i, v in Crate:GetChildren() do
	if v.ClassName == "Texture" then -- Finding already existing textures
		v.Texture = Image -- Setting each texture's "Texture" property to that of "Image" (which is also a texture)
	end
end

It might be good to write your code out more clearly like this:

for index,texture in Crate:GetChildren() do
	if texture.ClassName == "Texture" then
		texture.Texture = Image
	end
end

You can also just do for _,texture and you don’t need to use pairs anymore (it’s actually slower to use it). Additionally, you can also use texture:IsA('Texture') instead of texture.ClassName == 'Texture', but either are fine in this case.

3 Likes
local Image = Images[math.random(1, #Images)]

for i, v in pairs(Crate:GetChildren()) do
	if v.ClassName == "Texture" then
                local ImageClone = Image:Clone()
		ImageClone.Parent = v.Texture
	end
end

You probably want to set the Texture’s texture to the image instead of cloning the image too

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