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

Is there any way to shorten my code, which is about 60 lines long?

I'm making a gun and I've shortened my code to about sixty lines, but can I shorten the code more?

local tool = script.Parent.Parent
local pew = tool:WaitForChild('Pew')
local HEADSHOT = tool:WaitForChild('HEADSHOT')
local acceleration = -100
local damage = 15

tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		pew:Play()
		
		local bullet = game.ReplicatedStorage.Bullet:Clone()
		bullet.Parent = workspace.Bullets
		bullet.CFrame = script.Parent.CFrame + Vector3.new(0, 0.35, 0)
		bullet.Anchored = false
		bullet.Velocity = bullet.CFrame.lookVector * acceleration

		bullet.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild('Humanoid')
			local DB = hit:FindFirstChild('DB')
			
			if DB then
				bullet:Destroy()
			end
			
			if humanoid and humanoid.Parent.Name ~= tool.Parent.Name then
				if hit.Name == 'Head' then
					if HEADSHOT.IsPlaying == false then
						humanoid:TakeDamage(damage + 15)

						if hit.Neck then
							hit:FindFirstChild('Neck'):Destroy()
						end
						
						bullet.GotHimInTheHead.Parent = hit
						hit.GotHimInTheHead.HeadShotMessage.Text = 'HEADSHOT!'
						bullet:Destroy()
						HEADSHOT:Play()
						
					end
				
					humanoid:TakeDamage(damage + 15)
				else
					humanoid:TakeDamage(damage)
				end
				
				wait(2)
				
				if workspace.Bullets:FindFirstChild('Bullet') then
					workspace.Bullets.Bullet:Destroy()
				end
				
				if humanoid.Health <= 0 then
					if hit.Parent then
						hit.Parent:Destroy()
					end
				end
			end
		end)
	end)
end)

It'll help if you don't scream at me :l

3 comments

0

i dont think you can

0

For the most part, your code is clean. It doesn't really need much shortening. Also the lines on a script doesn't matter. If it really pains you, remove the empty lines in between things.

0

Thanks, but skipping lines is just my style. I seperate any lines that aren't like the one above it.

1 answer

No, i do not think you can. The only way i think you could shorten the code is remove empty lines and add comments where on the lines that were right below the empty lines This is how you comment, if you don't know

-- This is a comment!

2 comments

0

sorry i'm new to this thing

0

You can say this is an answer, but comments are basicly useless if you know what the code says. Unless you're storing 1000 lines of code as 1 line.

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