HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Elevator code for a game called ROBLOX

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calledrobloxelevatorgameforcode

Problem

Are there any ways I could improve speed and less code? The elevator that uses this script works fine. Could anything be better?

```
print(" Teknikk xPower 9700 PRE DEV V1 Intialised")

-- Develoment sample, May have functions added or removed. --
local Floor = script.Parent.Floor
local Floors = script.Parent.Floors
local FireLock = false
local Alarm = false
local Open = false
local Closed = true
local IsOpening = false
local IsClosing = false
local Moving = false
local Busy = false
local Locked = false
local DoorSpeed = 0.00001
local MotorStartSpeed = 0.13
local MotorStopSpeed = 0.13
local MotorSpeed = 12
local MotorCurrentSpeed = 8
local MoveDirection = "None"
local CallDirection = "None"
local FloorIndicatorOffset = 6
local LevelOffset = 3
local TargetFloor = 0
local TotalFloors = 0
local Car = script.Parent.Car.Control
local duck = false
local WaitCall = false
local CallQuene = {}

local CardLock = true
local CardNumber = {0,1}
local LockedFloors = {2,3,4,5,6,7,8}

function ProcessCall(xFloor, xDest)
if TargetFloor == 0 and xFloor ~= xDest then
if xDest > xFloor then
TargetFloor = xDest
Car.DirectionalIndicator.Decal.Texture = "http://www.roblox.com/asset/?id=119917350"
Start("Up")
end
if xDest < xFloor then
TargetFloor = xDest
Car.DirectionalIndicator.Decal.Texture = "http://www.roblox.com/asset/?id=119917359"
Start("Down")
end
end
end

function Start(xDirection)
Busy = true
if Open or IsOpening then
repeat DoorClose(Floor.Value) wait(0.1) until Closed == true and IsOpening == false
end
Moving = true
-- Some code for just 1 floor up, not too fast --
if (Floors:FindFirstChild("Floor"..TargetFloor).FloorLevel.Position - script.Parent.Car.Control.FloorLevel.Position).Magnitude < 14 then
MotorCurrentSpeed = 5
MotorStopSpeed = 0.05
LevelOffset = 5
else
MotorCurrentSpeed = MotorSpeed
MotorStopSpeed = 0.05

Solution

Indentation

It's in some places, but in the places it's not in, it's pretty bad.

You should always make sure your code is indented so, when looking over your code, you can see what goes where.

For example, your Start function has bad indentation. Generally, all function code between the signature and the end keyword of the function should have at least one indent (more if there are other structures like if)

How the beginning Start function should look:

function Start(xDirection)
    Busy = true
    if Open or IsOpening then
        repeat DoorClose(Floor.Value) wait(0.1) until Closed == true and IsOpening == false
    end

    ...


Also, I'm not sure if this is just personal preference, but you have a lot of "one-liners". By that, I mean that in a lot of places, you tend to try to put things on one line. While this does make your code look a bit more tidy, it might be harder for someone else to read it (or maybe even you, if you stepped away from the code for a while)

For example, I would change this line:

Car:FindFirstChild("DoorOpen").ClickDetector.MouseClick:connect(function() if not FireLock then DoorOpen(Floor.Value) end end)


To:

Car:FindFirstChild("DoorOpen").ClickDetector.MouseClick:connect(function()
    if not FireLock then
        DoorOpen(Floor.Value)
    end
end


Other than that, your code is very difficult to read, making it hard for people to give you a good review. Since this is a lot of code, I recommend finding an online program that can indent your LUA for you, as if you did it by hand, it could take a long time.

Code Snippets

function Start(xDirection)
    Busy = true
    if Open or IsOpening then
        repeat DoorClose(Floor.Value) wait(0.1) until Closed == true and IsOpening == false
    end

    ...
Car:FindFirstChild("DoorOpen").ClickDetector.MouseClick:connect(function() if not FireLock then DoorOpen(Floor.Value) end end)
Car:FindFirstChild("DoorOpen").ClickDetector.MouseClick:connect(function()
    if not FireLock then
        DoorOpen(Floor.Value)
    end
end

Context

StackExchange Code Review Q#29275, answer score: 7

Revisions (0)

No revisions yet.