Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there any way to make this script any shorter to reduce to confusion?

I'm new to scripting so scripts I make is sometimes too confusing to read. :(

local debounce = false

script.Parent.Equipped:Connect(function(mouse)
	local wield = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Wield)
	wield:Play()
	
		mouse.Button1Down:Connect(function()
			if not debounce then
				debounce = true
				local wield = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Wield)			
				local lunge = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Lunge)
				wield:Stop()
				lunge:Play()
				debounce = false		
			end
		end)
		
		mouse.Button1Up:Connect(function()
			if not debounce then
				debounce = true
				
				script.Parent.CanDamage.Value = true
				
				local wield = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Wield)					
				local slash = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Slash)
				slash:Play()
				wait(1)
				slash:Stop()
				wield:Play()
				
				script.Parent.Blade.Touched:Connect(function(hit)
					if script.Parent.CanDamage.Value == true then
						script.Parent.CanDamage.Value = false
						hit.Parent.Humanoid:TakeDamage(100)
					end
				end)
				wait(.1)
				
				debounce = false			
			end
		end)
end)

1 answer

✓ Accepted answer

I hope this is easier to read.

-- [ Debounces ] --
local debounce = false
local CanDamage = script.Parent:WaitForChild("CanDamage")

-- [ Animations ] --
local wield = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Wield)
local lunge = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Lunge)
local slash = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Slash)

-- [ Settings ] --
local Damage = 100

-- [ Events ] --
script.Parent.Equipped:Connect(function(mouse)
	wield:Play()

	-- [ Lunge ] --
	mouse.Button1Down:Connect(function()
		if not debounce then
			debounce = true
			wield:Stop()
			lunge:Play()
			debounce = false        
		end
	end)

	-- [ Slash ] --
	mouse.Button1Up:Connect(function()
		if not debounce then
			debounce = true
			CanDamage.Value = true                
			slash:Play()
			wait(1)
			slash:Stop()
			wield:Play()
			-- [ Damage ] --
			script.Parent.Blade.Touched:Connect(function(hit)
				if CanDamage.Value == true then
					CanDamage.Value = false
					hit.Parent.Humanoid:TakeDamage(Damage)
				end
			end)
			wait(.1)
			debounce = false            
		end
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.