Organizing a script full of OnClientEvent functions

Okay so basically i have a little GameHandler localscript for startercharacter, it functions right and perfectly however it does not look very clean and organized. Any ideas on how i can clean it up?

local HB = game.ReplicatedStorage.HB
local TweenService = game:GetService("TweenService")
local Gui = player.PlayerGui:WaitForChild("MainUI"):WaitForChild("BloodImage")
game.ReplicatedStorage.Events.UnDown.OnClientEvent:Connect(function() -- Undown effect 
	if Humanoid.Health > 0 then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) 
		warn("Debugging")
		TweenService:Create(
		Gui,
			TweenInfo.new(10),
			{ImageTransparency = 0.5 + (Humanoid.Health / Humanoid.MaxHealth)}
		):Play()
	end
end)
script.Parent.Events.IsDowned.OnClientEvent:Connect(function(VAL) -- Downed effect
	if VAL == "ON" then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		Gui.ImageTransparency = 0.4
		game:GetService("TweenService"):Create(game.Lighting.Blurred, TweenInfo.new(1), {Size = 15}):Play()
		df.Enabled = true
		HB:Play()
		if shift.Enabled == true then
			shift.Enabled = false
		end
		game:GetService("StarterGui"):SetCore("ResetButtonCallback", false)
		print("ON")
	elseif VAL == "OFF" then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

		--Gui.ImageTransparency = 1
		if shift.Enabled == false then
			shift.Enabled = true
		end
			df.Enabled = false
		game:GetService("TweenService"):Create(game.Lighting.Blurred, TweenInfo.new(1), {Size = 0}):Play()
		game:GetService("StarterGui"):SetCore("ResetButtonCallback", true)
		if Humanoid.Parent:FindFirstChild("Torso"):WaitForChild("Breath").IsPlaying then
			Humanoid.Parent:FindFirstChild("Torso"):WaitForChild("Breath"):Stop()  
			task.wait()
			Humanoid.Parent:FindFirstChild("Torso"):WaitForChild("Breath"):Destroy()
		end
		if HB.IsPlaying then
			HB:Stop()
		end
	end
end)```

I don’t know why I did this but ey I gotchu.
failedimage

local players = game:GetService("Players")
local rp = game:GetService("ReplicatedStorage") -- rename this if you're working with others probably
local TweenService = game:GetService("TweenService")
local StarterGui = game:GetService("StarterGui")



local player = players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Torso = Character:WaitForChild("Torso")



local HB = rp:WaitForChild("HB")
local Gui = player.PlayerGui:WaitForChild("MainUI"):WaitForChild("BloodImage")





rp.Events.UnDown.OnClientEvent:Connect(function() -- Undown effect 
	
	if Humanoid.Health > 0 then
		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) 
		warn("Debugging")
		
		TweenService:Create( 
			
			Gui,
			TweenInfo.new(10),
			{ImageTransparency = 0.5 + (Humanoid.Health / Humanoid.MaxHealth)}
			
		):Play()
	end
end)





script.Parent.Events.IsDowned.OnClientEvent:Connect(function(VAL) -- Downed effect
	
	if VAL == "ON" then
		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		Gui.ImageTransparency = 0.4
		TweenService:Create(game.Lighting.Blurred, TweenInfo.new(1), {Size = 15}):Play()
		df.Enabled = true
		HB:Play()
		
		if shift.Enabled == true then
			shift.Enabled = false
		end
		StarterGui:SetCore("ResetButtonCallback", false)
		print("ON")
		
	elseif VAL == "OFF" then
		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

		--Gui.ImageTransparency = 1
		if shift.Enabled == false then shift.Enabled = true end
		df.Enabled = false
		
		TweenService:Create(game.Lighting.Blurred, TweenInfo.new(1), {Size = 0}):Play()
		StarterGui:SetCore("ResetButtonCallback", true)
		
		local BreathAnimation = Torso:FindFirstChild("Breath")
		if BreathAnimation and BreathAnimation.IsPlaying then
			BreathAnimation:Stop()  
			task.wait()
			BreathAnimation:Destroy()
		end
		
		if HB.IsPlaying then
			HB:Stop()
		end
	end
end)
1 Like