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

I'm new to scripting, How do I change the decal on an object with a separate button?

So, the plan is to have a button that when pressed activates theses four walls and puts a countdown on them. How would I go about creating a script to make the button work, and change the decals on the walls? There are 4 separate decals for 3,2,1 and go. I don't know what I need to do to set up the decals and then script them.

1 answer

✓ Accepted answer

Hey scoremoresalmon... let me see if I can help you with your question. So let's examine what you want... you want a button, when pressed, will start a countdown and display different images on a Decal or Image.

Let me write you an example, and explain to you what is going on in that example, and you can mess around in studio and see what works for you.

local button = script.example_index
local decal = script.example_index

local decal_library = {
	[1] = "rbxassetid://example_assetId",
	[2] = "rbxassetid://example_assetId",
	[3] = "rbxassetid://example_assetId"
}

local debounce= true

button.MouseButton1Click:Connect(function()
	if (debounce) then 
		debounce = false

		for i = 3,1,-1 do
			local new_decal = decal_library [i]
			if (not new_decal) then 
				continue
			end
			
			decal.Texture = new_decal 
			wait(1)
		end

		debounce = true
	end 
end)

So for starters, let's examine what we set up. We created four variables, one being the button, the decal, a library of assetId's representing the images, and a debounce variable. From here, we set up an event, listening for a click on the button. We connected the event to a function that first checks the variable making sure this function does not run on top of itself, making sure it completes running before running again. From the first conditional, we move onto a for loop that countdown from three to one and indexes the image decal assetid based on the current index in the for loop, with the saved index in the library(i.e, array, table)... from here, we set the texture with the indexed texture and when completed, we set the debounce back to true, signaling we can run it again.

That might be a lot to take in, I do not know your level of experience at scripting, but hopefully, that helps. Feel free to message me if you have any more questions!

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