Advice to make the script better

I wanna know if there’s more efficient way to do this combat script, I want to check where did I did good and bad and advice that you can give me to make the script better looking and clean.

Code
wait()
local uis = game:GetService("UserInputService")
local cameraShaker = require(game.ReplicatedStorage.Modules.CameraShaker)
local Camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
--while char.Parent == nil do
--	char.AncestryChanged:Wait()
--end
local hum = char:WaitForChild("Humanoid")

--Animation
local blockingAnim = script:WaitForChild("BlockingAnim")
--print(char.Parent)
local gaurdingAnimation = hum:LoadAnimation(blockingAnim)
local Run = hum:LoadAnimation(script:WaitForChild("Run"))

local animation = Instance.new("Animation")
animation.Name = "Idle"
animation.Parent = script.Parent

animation.AnimationId = "http://www.roblox.com/asset/?id="
local animtrack = hum:LoadAnimation(animation)

script.Parent.Equipped:connect(function()
	animtrack:Play()
end)

script.Parent.Unequipped:connect(function()
	animtrack:Stop()
end)
----------------------------------------------------------------------------------------
--Remotes
local BlockingRemote = game.ReplicatedStorage.Fighting.Blocking
local RemoveBlocking = game.ReplicatedStorage.Fighting.RemoveBlocking
--local Blocking = game.ReplicatedStorage:WaitForChild("Fighting"):WaitForChild("Blocking")
local CombatStyle = game.ReplicatedStorage:WaitForChild("Fighting"):WaitForChild("Combat")
local CombatStyle2 = game.ReplicatedStorage:WaitForChild("Fighting"):WaitForChild("M2") -- will rename the vars later
--
local BlockCD = .3
local M2CD = 2.6
local M1CD = .34
local debounce = false
local LastTapped,Tapped = false,false

local currTime = 0
local prevTime = 0
local count = 0

local canAir = false
local State = nil

--local function isHolding()
--	return uis:IsKeyDown(Enum.KeyCode.W)
--end

---------------------------/Combat\--------------------------------------

-- M1
hum.StateChanged:Connect(function(oldState, newState)
	State = newState
	if newState == Enum.HumanoidStateType.Jumping then
		canAir = true		
	else
		canAir = false
	end
end)


local camShake =  cameraShaker.new(Enum.RenderPriority.Camera.Value,function(ShakeCF)
	Camera.CFrame = Camera.CFrame * ShakeCF
end)

game.ReplicatedStorage.EQ.CamShake.OnClientEvent:Connect(function(magnitude,roughness,fadeIn,fadeOut,lenght)
	camShake:Start()
	camShake:ShakeOnce(magnitude,roughness,fadeIn,fadeOut)
	wait(lenght)
	camShake:Stop()
end)

script.Parent.Activated:Connect(function()
	if char:FindFirstChild("CanAttack").Value == true and char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil and char:FindFirstChild("IsBlocking") == nil and char:FindFirstChild("BlockBreak") == nil then
		if debounce == false then
			debounce = true
			currTime = tick()
			local passedtime = currTime - prevTime
			camShake:ShakeOnce(1,1,1,1)
			if passedtime < 1 then
				------- Can Cuntinue the Combat Combo
				count += 1 
				if count > 5 then
					count = 1
				end
			else
				---Restarts the Combo	
				count = 1
			end	
			CombatStyle:FireServer(count,canAir,State)	
		end	
	end
end)


CombatStyle.OnClientEvent:Connect(function(plr,newcount)
	wait(M1CD)	
	prevTime = currTime
	debounce = false
end)	


-- M2
script.Parent.Equipped:Connect(function()
	uis.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2  then
			if debounce == false and char:FindFirstChild("CanAttack").Value == true and char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil and char:FindFirstChild("BlockBreak") == nil and char:FindFirstChild("IsBlocking") == nil then
				if  char:FindFirstChild("Combat") then
					local tool =  char:FindFirstChild("Combat")
					if tool:IsA("Tool")then
						debounce = true
						CombatStyle2:FireServer()	
					end
				end		
			end
		end
	end)
end)

CombatStyle2.OnClientEvent:Connect(function()
	wait(M2CD)
	debounce = false
end)

-- Block
uis.InputBegan:Connect(function(input, Typing)
	if not Typing then
		if input.KeyCode == Enum.KeyCode.F and debounce == false then
			if char:FindFirstChild("CanAttack").Value == true and char:FindFirstChild("BlockBreak") == nil and char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil then
				if char:FindFirstChild("Combat") then
					local tool = char:FindFirstChild("Combat")
					if tool:IsA("Tool")then
						debounce = true
						BlockingRemote:FireServer()
						game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 65}):Play()
						hum.WalkSpeed = 7
						gaurdingAnimation:Play()
						wait(BlockCD)
						debounce = false
					end
				end
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, Typing)
	if input.KeyCode == Enum.KeyCode.F then
		RemoveBlocking:FireServer()	
		game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70}):Play()
		gaurdingAnimation:Stop()
		hum.WalkSpeed = 16
	end
end)

RemoveBlocking.OnClientEvent:Connect(function()
	RemoveBlocking:FireServer()
	game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70}):Play()
	gaurdingAnimation:Stop()
	hum.WalkSpeed = 16
end)

-- Run
uis.InputBegan:Connect(function(input, Typing)
	if not Typing then
	if input.KeyCode == Enum.KeyCode.W then
			if Tapped == false then
				Tapped = true
			else			
				LastTapped = true
				Tapped = false
				
				Run:Play()
				game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 75}):Play()
				hum.WalkSpeed = 22
			end
			
			delay(.3, function()
				if Tapped then
					Tapped = false
				end
			end)
		end
	end
end)

uis.InputEnded:Connect(function(input, Typing)
	--if Typing then 
	if input.KeyCode == Enum.KeyCode.W and LastTapped and not Tapped then
			hum.WalkSpeed = 16	
			game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70}):Play()
			Run:Stop()
		--end
	end
end)

Please move this topic to #help-and-feedback:code-review, since the code works.


I don’t have an advice, I do my codes the same as you.

2 Likes

Usually you don’t want to use wait() as it is bad practice.

If you want to wait for you game to load simply do

if not game:IsLoaded() then
    game.Loaded:Wait()
end

This is just my opinion but I usually like writing out the variables, services and function names in PascalCase

2 Likes

Thank you for helping me out, idk if I put them in the correct category.

I will keep that in mind! thank you for replying.