i’m learning how to script, and i’ve run into a problem. the question is simple: is there a way i can do a function again from the start without calling the function again inside itself as that causes immense lag? thanks for any response.
Could you possibly clarify what you mean and give an example of what you’ve tried?
Can you give us an example of what you want? Can you write a piece of code to show us the behavior you want? (It doesn’t have to be perfect)
alright shy and @MrMouse2405
local music1 = game.Workspace.MiiChannelMusic
local music2 = game.Workspace.DoubleCherryPass
local musicplayer = game.StarterGui.ScreenGui.musicplayer
local clicked = 0
function playMusicOne()
music1.Playing = true
clicked += 1
end
function playMusicTwo()
music2.Playing = true
clicked += 1
end
function musicChanger()
music2.Playing = false
script.Parent.MouseButton1Click:Connect(playMusicOne)
script.Parent.MouseButton1Click:Connect(playMusicTwo)
music1.Playing = false
if clicked == 2 then
end
end
script.Parent.MouseButton1Click:Connect(musicChanger)
i have some code for a music changer and in the if clicked == 2 then part i want it to repeat the musicChanger function from the start
One question, why do you want to call musicChanger()
again?
so that once i click my button, the music will change from the Mii Channel Music to Double Cherry Pass.
So If I am correct, the logic behind your code is, First Click: Music 1, Second Click: Music 2, Third Click: Restart
yes! exactly. but i am stuck on the ‘restart’ part.
Well its easy, i can give you a trick to achieve this.
Make a variable, that stores either true or false. Everytime player clicks, you change the value of the variable to false if its true and vice versa. And you call a function that plays music 1 if true, and music 2 if false.
Here’s a trick:
local Value = true
Value = not Value --//Will make it false if its true and vice versa
thanks for this, but is there specific built in function in luau to do a function from its start?
i have now understood your solution, let me just build it
Here’s something I came up with (haven’t tested):
local songs = {
[1] = workspace.MiiChannelMusic,
[2] = workspace.DoubleCherryPass
}
local index = 0
script.Parent.MouseButton1Click:Connect(function()
index = index == #songs and 1 or index + 1
songs[index]:Play()
end)
seems a little complicated, can you explain it to me? the whole purpose of this was for me to learn
You can just call a function inside a function, this is known as Recursion.
local function A()
A() --That will run A from start.
end
--Or if you want lua build it globals
-- We have
spawn(fun) --This is not recommended for several reason
task.Spawn(fun) -- This is recommended
coroutine.wrap(fun)() -- Even this is recommended
--But remember, all these lua globals / coroutine methods run parallel to your code, It's like running them in another script.
it was causing IMMENSE lag for me
local songs = { -- Make a table with the index as a number so that you can index lateron
[1] = workspace.MiiChannelMusic, -- 1: your first song
[2] = workspace.DoubleCherryPass -- 2: your second song
}
local index = 0 -- make a variable so that you can mess with it when the button is clicked
script.Parent.MouseButton1Click:Connect(function() -- connect to the MouseButton1Click event
index = index == #songs and 1 or index + 1 -- set the index to (index + 1) if its less than #songs (number of songs in the table) or reset it to 1
songs[index]:Play() -- play the song
end)
I tried my best, I’m not great at explaining.
so by making the 1 and 2 in the table, what are we doing?
Yes, its because, its calling the function inside it self infinitely, just imagine it like this
A calls A(We will name second A as A1)
A1 calls A2
A2 calls A3
it goes infinitely.
Calling a function inside a function is known as Recursion.
You should add a break point for the function to stop calling itself infinitely.
local AlreadyCalled = false
local function A()
if not AlreadyCalled then
AlreadyCalled = true
A() --This will make it call once.
end
end
Now you can imagine the above code as this:
A – Checks if called – If no, calls A1, else doesn’t call
A1 – Checks if called – if no, calls A2, else doesn’t call
This also applies to lua globals… remember.
The [1]
and [2]
in the table is just so that the order doesn’t mess up just in case.
basically you’re telling the computer:
Here's a song for ya, keep it in position 1
Here's another, keep it in position 2
And by songs[index]
what we are doing is:
songs[1]:Play() -- if the index is 1, fetch the song with the index "1" in the table and play it
Actually took time to test, and I think I got it wrong lol.
Updated:
To adapt to my code:
local songs = {
[1] = workspace.MiiChannelMusic,
[2] = workspace.DoubleCherryPass
}
local index = 0
script.Parent.MouseButton1Click:Connect(function()
index += 1 -- increment the index
index = index > #songs and 1 or index -- if index is greater than the amount of songs, reset it to 1
songs[index]:Play()
end)
Make sure to mark any reply in this thread as a solution if it helped, don’t just leave the thread as unsolved even after getting a solution lol
You only need to call Connect
once. Otherwise each time the ‘Clicked’ event fires your functions will get called multiple times.
Here’s what I would do instead, which is hopefully easier to understand (but there are better ways to do this).
...
local clickCount = 0
local function onClicked()
if clickCount == 0 then
music1.Playing = false
music2.Playing = false
elseif clickCount == 1 then
music1.Playing = true
elseif clickCount == 2 then
music2.Playing = true
-- pause music1?
end
clickCount = (clickCount + 1) % 3
end
script.Parent.MouseButton1Click:Connect(onClicked)
You’ll notice that I add one to clickCount
and then perform % 3
on the result. This returns the remainder when dividing by 3 and as such prevents clickCount from exceeding 2.