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

How to make this event script shorter?

This script works 100% perfect, but now is my question is their a way to make of everything between the green line in a function or something such as adding more events without have to put each time that mount of lines. I prefer to make it short of possible because over 150 events are like 1650 lines.

Event script in workspace

game.Players.PlayerAdded:wait()

local player = game.Players.LocalPlayer

while true do
    local Players = game:GetService("Players")
    Choos = math.random(1) 
    wait(15)
    if Choos == 1 then
        wait(2)
--------------------------------------------------------- 
        if player.leaderstats.Level.Value >= 24 then 
        print("IT WORKS")
    	else
        if player.leaderstats.Level.Value <= 25 then
        local GuiEvent = script:findFirstChild("Easy"):Clone()
        GuiEvent.Parent = player.PlayerGui
        end
        end
--------------------------------------------------------- 
		game.ServerStorage:findFirstChild("DUCK"):clone().Parent = game.Workspace
		wait(500) 
		game.Workspace:FindFirstChild("DUCK"):Destroy()
        wait(5)
    end
end

2 comments

0

Remove the comments you'll shave 2 lines.

0

The green lines were to show you

2 answers

✓ Accepted answer

Tab your code correctly!

elseif

If you have just an if inside and else, then you should probably be using an elseif:

if player.leaderstats.Level.Value >= 24 then
	print("IT WORKS")
elseif player.leaderstats.Level.Value <= 25 then
	....

The conditions in this case are really quite strangely chosen. It would make much more sense to do it this way:

if player.leaderstats.Level.Value <= 23 then
	
else

end

workspace

The workspace variable holds game.Workspace. You shouldn't ever have to type out game.Workspace.

FindFirstChild

:findFirstChild is deprecated. You should be consistent and correct and use :FindFirstChild. Similarly, use :Clone() not :clone().

It is usually wrong to use . or : after :FindFirstChild -- it can return nil if it doesn't exist. And if you are expecting it to exist, there's no reason to use :FindFirstChild.

game.ServerStorage.DUCK:Clone().Parent = workspace
wait(500)
workspace.DUCK:Destroy()

Choos

You said you want it shorter. I don't know why you have the redundant Choos variable and the if checking it. It's always going to be 1.

LocalScripts

This script does not make sense.

You wait for PlayerAdded, but then grab the LocalPlayer. Thus this script will only work once someone else joins.

You then go on to use ServerStorage, which you can't use from a LocalScript.

I don't know what you're trying to do here, but it makes no sense.

Modular code
Just like LEGO

You can use these nifty nifty things called ModuleScripts which you can require in. If you move the functions out of your code and into ModuleScript returns, you can keep your code tidy and easy to sort through.

It also means that you can do simple dependency management or 'plugin' management.

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