Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script to cycle through decals every click?

Asked by 1 year ago

So what i want to do is making a script that cycles through a decal every time you press a button, also would like to have a button that goes back a decal every time you press it, so essentially like 2 buttons that can go forward or back a couple decals on a specific part. Thanks!

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

You can store all the decal textures in a table; cycle through them by getting the current index and incrementing it by 1 or -1.

local Decals = {
    "rbxasset://textures/SpawnLocation.png",
    -- Your decal textures here
}

local Decal = workspace.SpawnLocation.Decal -- Change to where your decal is located
local CurrentIndex = 1

local function NextDecal(increment)
    local newIndex = CurrentIndex + increment

    if newIndex == 0 then newIndex = #Decals end -- If the new index is at the start of the table it will go to the end

    if newIndex > #Decals then newIndex = 1 end -- If the new index is greater than the size of the table (indicating that it has reached the end of the table), it will reset back to the start.

    local newDecal = Decals[newIndex]

    CurrentIndex = newIndex
    Decal.Texture = newDecal
end

script.Parent.Forward.MouseButton1Click:Connect(function()
    NextDecal(1)
end)

script.Parent.Backward.MouseButton1Click:Connect(function()
    NextDecal(-1)
end)
Ad

Answer this question