Need help disabling run script when main menu is active

ive got a running script that changes the player’s FOV slightly. whenever the main menu is active, i want to make it so the running script is disabled whenever the menu is active. the script works, but it only works when the player joins; if they die and go back to the main menu, the running script doesnt disable again. how do i make it disable whenever the menu is active?

main menu script:

--code mostly taken from: https://www.youtube.com/watch?v=ecyKTDFOl5g&ab_channel=DevFlare, changes made by me
local CurrentCamera = workspace.CurrentCamera --get local players camera
local CameraPart = workspace:WaitForChild("menucamerapart") --get the camera part
local PlayButton = script.Parent.Frame.PlayButton --get the actual play button on the frame
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") --get local player's humanoid
local Frame = script.Parent.Frame --get main menu frame
local RunScript = game.StarterPlayer.StarterCharacterScripts.RunScript

local spawnpads = workspace.spawnpads:GetChildren() --get spawn locations
--wait(0.001) --no clue why this is here tbh

CurrentCamera.CameraType = Enum.CameraType.Scriptable --allow camera to be controlled by scripts
CurrentCamera.CFrame = CameraPart.CFrame --set camera's cframe to the cframe of the camera part
Frame.Visible = true --make frame visible (just easy to work on studio with this, dont gotta hide ui n stuff)
Humanoid.WalkSpeed = 0 --player cant move when main menu is on
Humanoid.JumpPower = 0 --player cant jump when main menu is on
RunScript.Enabled = false
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) --hide the backpack from player
game:GetService("StarterGui"):SetCore("ResetButtonCallback", false) --disable resetting in main menu

function Play()
	CurrentCamera.CameraType = Enum.CameraType.Custom --set camera back to default settings
	Humanoid.WalkSpeed = 24 --let player move
	Humanoid.JumpPower = 50 --let player jump
	RunScript.Enabled = true
	
	local selectedspawn = spawnpads[math.random(1, #spawnpads)] --select random spawn from spawnpads folder
	Humanoid.Parent:MoveTo(selectedspawn.Position) --tp player on the map from spawn box
	
	game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) --show the backpack again
	game:GetService("StarterGui"):SetCore("ResetButtonCallback", true) --allow resetting
	script.Parent:Destroy() --remove the main menu
end

PlayButton.MouseButton1Click:Connect(Play) --run it
3 Likes

Wouldn’t it be because your code and gui and the is destroyed?

Is this the entire script or is there a function for when the gui is enabled? Wait nvm try putting it in startercharacterscripts

they are destroyed, but whenever a player dies the menu sort of just appears again, im not really sure why tbh since i got parts of the code from a tutorial video

you can try this:

humanoid.Died:Connect(function()
	RunScript.Enabled = false
end)

It will cause the runscript to be deactivated every time the player dies.

where do i put this, i tried putting it inside the Play() function and outside of it and it didnt seem to work

put it like this

function Play()
	CurrentCamera.CameraType = Enum.CameraType.Custom --set camera back to default settings
	Humanoid.WalkSpeed = 24 --let player move
	Humanoid.JumpPower = 50 --let player jump
	RunScript.Enabled = true
	
	local selectedspawn = spawnpads[math.random(1, #spawnpads)] --select random spawn from spawnpads folder
	Humanoid.Parent:MoveTo(selectedspawn.Position) --tp player on the map from spawn box
	
	game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) --show the backpack again
	game:GetService("StarterGui"):SetCore("ResetButtonCallback", true) --allow resetting
	script.Parent:Destroy() --remove the main menu
end

humanoid.Died:Connect(function()
	RunScript.Enabled = false
end)

PlayButton.MouseButton1Click:Connect(Play) --run it

The reason why it does not work within the play() function is because it is only executed when the player presses the play button.

1 Like

tried this and it still doesnt seem to work

also another thing i just found out is that it turns out the thing i have to disable the script upon joining doesnt even work; i thought it did because i didnt click on the game window first before clicking play :skull:

Rather than destroying the main menu gui u can just set its visible property to false. then in your sprinting script you should check if the menu gui is visible, if it is end the function

1 Like

i put this in the running script:

while task.wait(0.1) do
	print(mainmenu.Visible)
end

for some reason it only prints “false” even if the gui is visible

also the variable mainmenu is for the gui’s frame

is mainmenu pointing to the StarterGui or the PlayerGui?

1 Like

local mainmenu = game.StarterGui.MainMenuGUI.Frame

MainMenuGUI is a ScreenGUI object

yeah but you’re pointing to the StarterGui, you want to check the playergui of the local player

1 Like

i figured out how to get the local player’s main menu frame and the print statement works as it should (it actually prints “visible” when the menu is on now)

how do i make it so the running script can only work when the frame isnt visible? do i need to put the UIS functions for it in a huge while loop + if statement or something?

same but put it in a normal non-local script and make it inside startercharacterscript

local Character = script.Parent
local Humanoid = script.Parent:WaitForChild("Humanoid")
local RunScript = game.StarterPlayer.StarterCharacterScripts.RunScript

Humanoid.Died:Connect(function()
       RunScript.Enabled = false
end)

nope. all you need to do is start your sprint function with:

if mainmenu.Visible then
    return
end

return is used to end functions early

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.