Function works once

Hey there!

PLEASE NOTE THAT THIS PROJECT IS JUST FOR TESTING; I WILL RE WRITE THE CODE AND RE BUILD EVERYTHING TO MAKE IT ORGANISED WHEN THE SYSTEM WORKS.

So i’m making a main menu script where the player can go from the main menu to the intermission screen. Everything was working fine until i added a physical ‘back’ button that worked perfectly but somehow only made a function run once. The function is there to take the player out of the main menu camera loop and put his camera CFrame on the intermission screen by simply clicking a ‘play’ button.

The script below shows you a local script with the loop, and the remote event being detected (when the player goes ‘back’) causing the function to run.

Here’s the script:

local deb = true

local function MainMenuCamera()
	while deb == true do
		script.Parent.MouseButton1Click:Connect(function()
			deb = false
			print("Yes")
			Animation1:Cancel()
			Animation3:Cancel()
			camera.CFrame = workspace.CameraE1.CFrame
			script.Parent.Parent.Parent.Parent.IntermissionButtons.Enabled = true
	    end)
		camera.CFrame = Camera1.CFrame
		Animation1:Play()
		Animation1.Completed:Wait()
		camera.CFrame = Camera5.CFrame
		Animation3:Play()
		Animation3.Completed:Wait()
		print("Yes2")
	end
end

MainMenuCamera()

game.ReplicatedStorage.BackArrow.OnClientEvent:Connect(function()
	deb = true
	MainMenuCamera()
	print("Bruh")
end)

Any help would be really appreciated! :+1:

1 Like

I think you use deb for a debounce. However, you never set deb to true at the end. It says while deb == true do, but if deb is never true it won’t run. It only runs once because deb is only true once.

2 Likes

You didn’t set debounce to true at the end which means it will only run once, I can only see that you set debounce to true on the local script but that won’t work. Just set the debounce to true when the function has finished so that the debounce can allow the function to run again.

1 Like

(@Tamzy3D, @LoveTheBears101)

But i set deb to true before i run the function again in:
game.ReplicatedStorage.BackArrow.OnClientEvent:Connect(function()

Also the loop works fine, its just that the:

script.Parent.MouseButton1Click:Connect(function()

won’t actually trigger for some reason.

(Also the deb isn’t a debounce its a value that stops the loop completely when the ‘Play’ button is pressed)

you only called the function once set it in a while loop

local deb = true

local function MainMenuCamera()
	while deb == true do
		script.Parent.MouseButton1Click:Connect(function()
			deb = false
			print("Yes")
			Animation1:Cancel()
			Animation3:Cancel()
			camera.CFrame = workspace.CameraE1.CFrame
			script.Parent.Parent.Parent.Parent.IntermissionButtons.Enabled = true
	    end)
		camera.CFrame = Camera1.CFrame
		Animation1:Play()
		Animation1.Completed:Wait()
		camera.CFrame = Camera5.CFrame
		Animation3:Play()
		Animation3.Completed:Wait()
		print("Yes2")
	end
end

while wait(.5) do
    MainMenuCamera()
end

The problem isn’t the ‘MainMenuCamera()’ not running, its the: script.Parent.MouseButton1Click:Connect(function()
not running.

I don’t really see how setting a while loop in the OnClientEvent function would fix the problem. In addition the ‘MainMenuCamera()’ already has a while loop in it so i don’t see why i should add wrap another while loop on it.

I may have misunderstood what you were trying to say, if so let me know! :+1:

Sorry About That you should fire the function on an event

like this

local function Buh()
     print("Bacons i like")
end

script.Parent.MouseButton1Click:Connect(function()
    Buh()
end)

Or you could try

local function Buh()
print("Bacons i like")
end

script.Parent.MouseButton1Click:Connect(function()
if deb == true then
deb = false
Buh()
end
end)

@IocaIe

I used print to find out what was running and what wasn’t. For some reason even if i use that method, the script.Parent.MouseButton1Click:Connect(function() just wont fire at all (It doesn’t print ‘Yes’. Everything else works, just not this simple function…
Iv’e also tried re-ordering everything in the function such as putting deb = false at the bottom of the function and in the middle, but that just messes up the system completely.

I don’t think you can connect a function, inside of a function, my code checks if debounce is true.

Edit: Also I prefer MouseButton1Down as it works for mobile also.

Doesn’t MouseButton1Down connect when the user just clicks, without having to actually have the cursor on the button?

Did you run it on a server script?

No. It’s pretty much MouseButton1Click except it works for mobile.

Going off topic, but you are creating new mousebutton1click event everytime it loops, which could go on a long time. Which is generally in my mind, is a bad practice.

This is all on a local script under a button. I don’t think the server and the client stuff has anything to do with this. By the way,

game.ReplicatedStorage.BackArrow.OnClientEvent:Connect(function()
	deb = true
	MainMenuCamera()
	print("Bruh")
end)

works perfectly fine.

Is there anyway i can prevent this from happening? Maybe using coroutines?

Can’t you just check if the player is not in the menu? Ex. Add a function outside the loop (I suggest before the loop, unless you want to use coroutines.)

local OnMenu = false

ButtonToCloseMenu.MouseButton1Click:Conenct(function()
OnMenu = false
end)

OtherButtonToOpenMenu.MouseButton1Click:Connect(function()
OnMenu = true
end)

while OnMenu == true dO
--stuff
end
1 Like

Here is a fixed version hopefully

local deb = true

local function MainMenuCamera()
	while deb do
		wait(1)
		deb = false
		print("Yes")
		Animation1:Cancel()
		Animation3:Cancel()
		camera.CFrame = workspace.CameraE1.CFrame
		script.Parent.Parent.Parent.Parent.IntermissionButtons.Enabled = true
	    
		camera.CFrame = Camera1.CFrame
		Animation1:Play()
		Animation1.Completed:Wait()
		camera.CFrame = Camera5.CFrame
		Animation3:Play()
		Animation3.Completed:Wait()
		print("Yes2")
	end
end


script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.BackArrow.OnClientEvent:Connect(function()
		deb = true
		MainMenuCamera()
		print("Bruh")
	end)
end)

I need to know where you fired your event from the server it’s just maybe one of the problems

Yes, move the event outside of your function, and add a

if deb == true then
--rest of your code
end

no coroutines is not helpful here.

1 Like

That’s just what i have at the moment without the Animation Cancelling wrapped in a function which would really mess up the whole system.

Thanks for trying tho, i appreciate the effort! :+1: