So, currently I been re-looking at my meditation system and I'm just curious how I could improve at this?

I really just want to improve this.

-- \\ Player-Related Variables // --

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local HUM = Character:WaitForChild("Humanoid")


-- \\ Variables // --

local Randomzier = Random.new()

local Meditation1 = false

local GUIS = script.Parent

local Circle = GUIS.MovingCircle

local White = Color3.fromRGB(255, 255, 255)


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

local UIS = game:GetService("UserInputService")

local Debris = game:GetService("Debris")

local TW = game:GetService("TweenService")

local RS = game:GetService("ReplicatedStorage")

local Med = RS:FindFirstChild("Meditation")

local MouseLoc = UIS:GetMouseLocation()

local Lighting = game:GetService("Lighting") -- Instance that will be Tweened

local TweenService = game:GetService("TweenService") -- Service that allows us to use Tweens

local Particle = RS.ParticleEmitters.lsls:Clone()

Particle.CFrame = Humrp.CFrame * CFrame.new(0, 2 , 0)

local Weld = Instance.new("ManualWeld")
Weld.Part0 = Particle
Weld.Part1 = Humrp


-- \\ Animation-Related Variables //--

local Anim = script.Meditation



-- \\ Functions // --

GUIS.IgnoreGuiInset = true

UIS.InputBegan:Connect(function(Input, Processed, IsTyping)
	local Player = game.Players.LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait()
	
	
	
	local OGTP = TweenInfo.new(
		2,
		Enum.EasingStyle.Exponential,
		Enum.EasingDirection.Out,
		0,
		false,
		1
	)
	
	local TP = TweenInfo.new(
		5,
		Enum.EasingStyle.Exponential,
		Enum.EasingDirection.Out,
		-1,
		true,
		1		
	)
	
	
	local OGGoals = {
		Density = 0.3,
		Offset = 0.25,
		Decay = Color3.fromRGB(106, 112, 125),
		Glare = 0,
		Haze = 0,
	}
	
	local OGGoals2 = {
		FarIntensity = 0.104,	
		FocusDistance = 4.16,		
		NearIntensity = 0.75
}
	
	local Goals = {
		
		Density = 0.613,
		
		Offset = 0.081,
		
		Decay = Color3.fromRGB(175, 185, 206),
		
		Glare = 0.32,
		
		Haze = 2.26
	}
	
	local Goals2 = {
		
		FarIntensity = 0.5,
		
		FocusDistance = 0,
		
		NearIntensity = 0.35
		
		
	}
	---------------------------------------------------------------------------
	local TWAtmo = TweenService:Create(Lighting.Atmosphere, TP, Goals)
	local TWBlur = TweenService:Create(Lighting.DepthOfField, TP, Goals2)
	local OGTWAtmo = TweenService:Create(Lighting.Atmosphere, OGTP, OGGoals)
	local OGTWBlur = TweenService:Create(Lighting.DepthOfField, OGTP, OGGoals2)
	local MedAnim = Character.Humanoid.Animator:LoadAnimation(Anim)
	----------------------------------------------------------------------------
	if Meditation1 == true then return end
	
	if not IsTyping then
		
		if Input.KeyCode == Enum.KeyCode.M then			
			if GUIS.Enabled == false then				
				GUIS.Enabled = true

		Circle.MouseEnter:Connect(function()
					if Meditation1 == false then
					Meditation1 = true
						
					HUM.WalkSpeed = 0
						
					--local PR = RS.Sit:Clone()
					
					local function TweenDone(state)
						if state == Enum.TweenStatus.Completed and Meditation1 == true then
								RS.Progressing:FireServer("MeditationTraining")
								print("Yeah we good")
							else 
								return
							
						end
					end
					--print("We working")
					MedAnim:Play()
					MedAnim.Looped = true
					
					TWAtmo:Play()
					TWBlur:Play()
						Part = Instance.new("Part",workspace)
						Part.Size = Vector3.new(2048, 16, 2048)
						Part.Position = Vector3.new(0, 333, 0)
						Part.Transparency = 1
						Part.Massless = true
						Part.Anchored = true
						Part.Name = "It's Raining outside..."
						
						local Emitter = Instance.new("ParticleEmitter", Part)
						Emitter.Rate = 700
						Emitter.Lifetime = NumberRange.new(2, 5)
						Emitter.Size = NumberSequence.new(0.8,2)
						Emitter.Color = ColorSequence.new{
							ColorSequenceKeypoint.new(0, White),
							
							ColorSequenceKeypoint.new(1, White)
						}
						Emitter.SpreadAngle = Vector2.new(-360, 360)
						Emitter.Acceleration = Vector3.new(0, -60, 0)
						
					
					--print("Pocket full of ducks")
						
						--PR.Parent = Humrp
						
						
					while GUIS.Enabled do
						task.wait(1)
							script.Parent.MovingCircle:TweenPosition(UDim2.new(Randomzier:NextNumber(0, 0.962),46 ,Randomzier:NextNumber(0, 0.285),47), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, math.random(5,8), false, TweenDone)
							task.wait(3)
							Particle.Parent = workspace
							TWAtmo:Pause()
							TWBlur:Pause()
						end
					end		
				
				end)
				
				Circle.MouseLeave:Connect(function()
					MedAnim:Stop()
					TWAtmo:Cancel()
					TWBlur:Cancel()
					OGTWAtmo:Play()
					OGTWBlur:Play()
					Part:Destroy()
					GUIS.Enabled     = false
					HUM.WalkSpeed = 16
					Meditation1      = false
				
					end)
				end
			end
		end
	end)

Your code has a memory leak in it. When you are connecting the Circle.MousEnter and Circle.MouseLeave. What i would do, is put those events at the top of the script, and have a variable holder to see if it’s enabled and if it is then we let the event continue.
Something like this

local TriggerEnterLeave = false

-- on input or anywhere else
TriggerEnterLeave = true

MousEnter:Connect(function()
    if not TriggerEnterLeave then return end -- stop the function here because they can't run it now
    -- do your stuff here
end)

MouseLeave:Connect(function() --[[ set TriggerEnterLeave to false ]] end

It looks like you’re using the Meditation1 variable in your code, so I guess you could move all that at the top without any change (along with moving the tween informations).

Personally, I have an event module where I manage connections on instances and I can manually trigger/disconnect them easily. I would suggest this approach if you fear memory leaks or do them often.

I would also suggest moving the tween informations outside of the InputBegan event. But that’s up to you.

It would be nice if some of it was handled on the server too, because from what I see, I can fire RS.Progressing:FireServer("MeditationTraining") and gain unlimited stats (unless you have a debounce on the server. But it would still be exploitable in some way.).

Also, a question. Is this script in StarterCharacter?

TL;DR: Good organization, but there are few issues such memory leak and a vulnerability.

This is StarterGUI.

So with this, what you trying make the script do is stop so once the MouseLeave function is done, it cannot be ran again whilst the MouseLeave event is fired?

1 Like

Basically yeah, what your code was doing is connecting a new MousEnter and MouseLeave every time M was pressed. So there would have been 5 events connected when pressed M 5 times.

1 Like

Ohhh, thank you so much for pointing it out. I really like to avoid memory leaks in my scripts, but I believe sometimes I unknowningly do it.

1 Like

Actaually from doing this, I seem to have come across a error in which the ball doesn’t move at all and only the part where the gui screen gets disabled seems to work.