How would I make this toggleable by a keybind?

Currently, I decided to drop the current Meditation system and ask

-- \\ Variables // --

local Randomzier = Random.new()

local Meditation = Instance.new("BoolValue")

local GUIS = script.Parent

local Circle = GUIS.MovingCircle

-- \\ Services/Service-Related Variables // --

local UIS = game:GetService("UserInputService")

local TW = game:GetService("TweenService")

local RSR = game:GetService("RunService")

local RS = game:GetService("ReplicatedStorage")


local MouseLoc = UIS:GetMouseLocation()
-- \\ Functions // --

GUIS.IgnoreGuiInset = true



	Circle.MouseEnter:Connect(function()
		print("We working")
		if Meditation == true then return end
		while true do
			wait(1)
			script.Parent.MovingCircle:TweenPosition(
				UDim2.new(Randomzier:NextNumber(0, 0.962),0 ,Randomzier:NextNumber(0, 0.285),0),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Linear,
				5,
				false,
				nil
			)
			wait(1)
		end


	end)

	Circle.MouseLeave:Connect(function()

		GUIS.Enabled = false

	end)

How would I make an event such as this toggleable even though it resides in here?
image

I want to use a StarterPack local script as well to toggle it so, I’m kind of stuck on this.

Look at this article. Hope this helps.
https://developer.roblox.com/en-us/api-reference/class/UserInputService

Could I simply put that in the current script and it would work with no issues?

okay so here’s a few things wrong with your script;

so in the very 1st of the script you create a variable named “Meditation” and used Instance.new to create a bool value? but in this line here you checked if a BoolValue is equal to true?
i think you were trying to write; BoolValue.Value == true maybe? but yet i dont understand it’s use since its value nor the medation variable is even being changed.

also inside the MouseEnter event you decied to put this while loop inside of it? okay so the issue with this is that even while the gui is disabled, the loop will still run, so why dont run the loop while only the GUIS.Enabled is equal to true so instaid you should have while GUIS.Enabled do since the event will still stop when the gui is enabled property is set to false.

a StarterPack localScript? i mean you can probably use BindableEvent to communicate from client to client maybe?

you want it to be toggleabe? well you can use Variables to make it toggleable for example (im gonna use a keybind toggle script cuase of yeah):

note: this is a “EXAMPLE” script so please dont copy and paste it since i kinda speeded trought it -.-

local UIS = game:GetService("UserInputService")
local Toggle = false -- defualt to false

UIS.InputBegan:Connect(function(obj)
     if obj.KeyCode == Enum.KeyCode.E then
       Toggle = not Toggle --makes it the oppsite of what it currently is (making the toggling effect)
           if Toggle then
               print("IS ON")
           else
             print("Is Off")
           end
       end
     end
end)

as in the example above i used UserInputService.InputBegan and checked for the InputObject’s KeyCode for the player’s key.

my question is, are you still gonna wait to use the MouseEnter and MouseLeave event to
also work with the tool AND the users input? if you wanna do that i recomend you put these into functions like this:

note: again this is an example i dont think you should be copy and pasting this cuase uh all it does
is print :3

local GUIS = script.Parent

local function UpdateMaditateGui(Bool)
	if Bool then
		print("do madeitaiont loop")
	else
		print("stop ")
	end
end

so you can just fire The function to toggle the maditation. i mean if your gonna use multple ways to toggle it?

(i may have done things wrong but eh)

1 Like
  1. The usage of the Meditation boolean was to disable my combat (which is in another place, as I have a place where I test very experimental builds/scripts) I didn’t want combat to be activated during the time of meditation so that meditation Bool is tossed for a later use.

  2. Thank you so much for pointing out the loop still running even if the GUI was disabled, that would’ve probably caused some memory issues/lag.

  3. I just plan to toggle it once, via the M key, and then close it out if the player fails the meditation prompt. I also do need the mouse to track if the mouse is on the moving ball.

  4. I tried researching BindableEvents, but feel stumped what I would be putting in the server script mid-way, and if it is the script in the current local gui, then what would be in the LocalScript on the other end since the main function isn’t in the gui anymore.

Fixed, decided to simply put the toggleable meditation script into the guis script.