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

Any way to make this more efficient?

Asked by
lomo0987 250 Moderation Voter
10 years ago
function Respawn(p)
wait()
if game:GetService("GamePassService"):PlayerHasPass(p, 157723808) then  
local a = game.Lighting:FindFirstChild("DH")
if a ~= nil then
local b = a:GetChildren()
for i=1, #b do
b[i]:Clone().Parent = p.Backpack
end
end
end
end

function Enter(p)
wait()
if game:GetService("GamePassService"):PlayerHasPass(p, 157723808) then
local c = game.Lighting:FindFirstChild("DH")
if c ~= nil then
local d = c:GetChildren()
for i=1, #d do
d[i]:Clone().Parent = p.Backpack
end
end
p.CharacterAdded:connect(function(Character)Respawn(p)end)
end
end

game.Players.ChildAdded:connect(Enter)

Any way to make this more efficient?

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

The only way to make this more efficient is to use the PlayerAdded event instead of the ChildAdded event. There are, however, numerous ways to make your code more readable. Readability is arguably even more important than functionality.

Tabbing

First a foremost, be sure to tab you code correctly. Every time you have a loop, function, or if statement you should have that loop, function, or if statement followed by a single end, and then everything inside between the beginning of the loop, function, or if statement and its end should be tabbed once. For example, this piece of code is tabbed correctly:

local num1 = 1
local num2 = 10
local bool = true

function check()
    for i = num1,num2 do
        if bool then
            print(it's true)
        end
    end
end

This way, it is much easier to read and also to see what end goes with what.

Variables

You should write out variables so anyone looking at your code will know what they mean. Abbreviations are fine, but you should leave enough of a clue so everyone will understand them. For example, cam would be a perfectly fine name to put for the variable of a camera, but simply putting c would not be as good.

In Lua, variables are usually created in one of these two formats:

local lower = 1
local lowerUpperUpper = 2

With the first word lower case and all others upper case. This is the style I prefer. Another option is this:

local word_anotherword_anotherword = 2

With underscores separating multiple words.

You should also always use local variables whenever possible. No only are they more efficient, but as soon as someone see the local key word they immediately know that a variable is about to be defined, making your code more readable.

Functions

You need to know when to use anonymous functions and when not to. In the case of PlayerAdded and CharacterAdded events, anonymous functions are usually better. Instead of clever use of connections with regular functions, you can simply put one anonymous function inside the other, making your code easily read.

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(chr)
        --code
    end)
end)

I hope I helped! If you have any more questions feel free to comment!

Ad

Answer this question