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

How to make a value go up to 9 then start back at 1 in a MouseClick?

Asked by 4 years ago
Edited 4 years ago
local number = 1
local clickDetector = script.Parent
clickDetector.MouseClick:Connect(function()
local wChildren = game:GetService("Workspace"):GetChildren()


    for i,c in pairs(wChildren) do
        if string.sub(c.Name, 1, 2) == "FM" then
    c.Parent = game.Lighting
local fm = 'FM'..number 

            if game:GetService("Lighting"):FindFirstChild(fm) then
local fm1 =    game:GetService("Lighting"):FindFirstChild(fm)
fm1.Parent = game:GetService("Workspace")
print(fm)
number = number + 1
else
local number = 1
print('FM'..number)

end
end
end
end)

the problem is that it won't continue with the if loop, it just stops in the else statement, but I want it to go with the loop this is the entire output FM1 FM2 FM3 FM4 FM5 FM6 FM7 FM8 FM9 FM1

why doesnt it go back to FM1 and then loop the output again?

0
You need to check for when "number" equals 9, then set it back to 1. FishslayerX 8 — 4y
0
No. There is 9 folders in my Lighting and I already do that by checking if there is a 10th folder or not, but there isn't, so it is supposed to start the 1-9 loop again from 9. Dan_PanMan 227 — 4y
0
what are you trying to do? TheluaBanana 946 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

As far as I can tell, there is nothing wrong with your script apart from line 18 where you said local number = 1. Presumably you would go from 1 - 9 and subsequently be stuck there? Im gonna include an example for cycling through a list anyways(place in a part btw):

local part = script.Parent
local n = 0

local clickD = Instance.new("ClickDetector", part)

local list = {"fm1","fm2","fm3","fm4","fm5","fm6","fm7","fm8","fm9"} -- hyperrealistic replica of sounds

clickD.MouseClick:Connect(function()
    if n < 9 then
        n = n + 1
    else
        n = 1
    end

    print(list[n])
end)
0
Lines 8-16 gave me the wanted output, really thank you for that! Dan_PanMan 227 — 4y
Ad

Answer this question