Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

0
i dont think you can Flasker917 21 — 3y
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. SpiralRBX 224 — 3y
0
Thanks, but skipping lines is just my style. I seperate any lines that aren't like the one above it. huntergamind 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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!
0
sorry i'm new to this thing Bigmancozmo 7 — 3y
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. huntergamind 2 — 3y
Ad

Answer this question