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

How do i make this script's code look neater?

Asked by 7 years ago

How do i make the script below Neater?

--[[--[[--[[--[[
Setup Everything
--]]--]]--]]--]]
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local HotbarItems = {'Grass','Stone','Dirt','Sand','TorchWall','MoltenRock','Netherrack','Water','Lava'}
local ItemList = {'SpawnZombie','Fire','NetherPortal','Grass','Dirt','Stone','Bedrock','DiamondOre','CopperOre','IronOre','EmeraldOre','GoldOre','RedstoneOre','MoltenRock','Bricks','Leaves','WoolDarkGray','WoolOrange','WoolGray','WoolBrown','WoolDarkGreen','WoolLightGreen','WoolGreen','WoolBlue','WoolLightBlue','WoolLightYellow','SpruceWood','OakWood','DarkOakWood','AcaciaWood','AcaciaWoodPlanks','DarkOakWoodPlanks','OakWoodPlanks','SpruceWoodPlanks','Concrete','Cobblestone','Sand','Gravel','Netherrack','WoolRed','Water','Lava'}
local Selected = 1
game:GetService('Players').LocalPlayer.CharacterAdded:connect(function()
game:GetService('Players').LocalPlayer.Character:WaitForChild('Humanoid')
PunchAnim = game:GetService('Players').LocalPlayer.Character.Humanoid:LoadAnimation(script.Punch)
PlaceAnim = game:GetService('Players').LocalPlayer.Character.Humanoid:LoadAnimation(script.Place)
end)
local Rank = game:GetService('DataStoreService'):GetDataStore('PlayerRanks')
local PlayerShirt = GetADataStore('UWPlayerShirt')
local PlayerPants = GetADataStore('UWPlayerPants')
--[[--[[
Functions
--]]--]]
function CreativeItemClick(Button)
    Button.MouseButton1Click:connect(function()
        HotbarItems[Selected] = Button.ItemTag.Value
    end)
end
function GetADataStore(DataStoreName)
    local DataStore = game:GetService('DataStoreService'):GetDataStore(DataStoreName)
    return DataStore
end
--[[
Chat
--]]
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

game:GetService('Players').LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if key == '/' then
        script.Parent.Visible = true
        script.Parent.ChatBar:CaptureFocus()
    end
end)


game:GetService('Players').LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if key == Enum.KeyCode.Return then
        if script.Parent.ChatBar.Focused then

        end
    end
end)
--[[--[[--[[-
Hotbar/Blocks
--]]--]]--]]-
mouse.Button1Down:connect(function()
    local target = mouse.Target
    PunchAnim:Play()
    if target.Parent:FindFirstChild('Humanoid') then
        target.Parent.Humanoid:TakeDamage(10)
        if target.Paremt.Torso:FindFirstChild('Damaged') then
        target.Parent.Torso.Damaged:Play()
        else
        local Damaged = game:GetService('Lighting').Damaged:Clone()
        Damaged.Parent = target.Parent.Torso
        target.Parent.Torso.Damaged:Play()
        end
    end
    for index, value in ipairs(ItemList) do
    if target.Name == value then
    target:Destroy()
end
end
end)

mouse.Button2Down:connect(function()
    PlaceAnim:Play()
    local PlacedObjectIdentifier = HotbarItems[Selected]

    if HotbarItems[Selected] == 'SpawnZombie' then
    local Zombie = game:GetService('Lighting').Zombie:Clone()
    local p=mouse.Hit.p
    local x,y,z = p.x,p.y,p.z

-- Added stuff
    xdif = math.floor(x/4+.5)*4  -- Grids positions X value (0,4,8,12,16 etc)
    ydif = math.floor(y/4+.5)*4  -- Grids positions Y value (0,4,8,12,16 etc)
    zdif = math.floor(z/4+.5)*4  -- Grids positions Z value (0,4,8,12,16 etc)

    Zombie:MoveTo(Vector3.new(xdif,ydif,zdif))  
    else
    local ClonedBlock = game:GetService('Lighting'):FindFirstChild(PlacedObjectIdentifier):Clone()

    ClonedBlock.Parent = game:GetService('Workspace')
    local p=mouse.Hit.p
    local x,y,z = p.x,p.y,p.z

-- Added stuff
    xdif = math.floor(x/4+.5)*4  -- Grids positions X value (0,4,8,12,16 etc)
    ydif = math.floor(y/4+.5)*4  -- Grids positions Y value (0,4,8,12,16 etc)
    zdif = math.floor(z/4+.5)*4  -- Grids positions Z value (0,4,8,12,16 etc)

    ClonedBlock.CFrame = CFrame.new(xdif,ydif,zdif)
    end
end)
--[[--[[
Functions
--]]--]]
mouse.KeyDown:connect(function(key)
    if key == '1' then
        Selected = 1
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0,0,0,0))
    elseif key == '2' then
        Selected = 2
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.1,0,0,0))
    elseif key == '3' then
        Selected = 3
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.2,0,0,0))
    elseif key == '4' then
        Selected = 4
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.3,0,0,0))
    elseif key == '5' then
        Selected = 5
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.4,0,0,0))
    elseif key == '6' then
        Selected = 6
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.5,0,0,0))
    elseif key == '7' then
        Selected = 7
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.6,0,0,0))
    elseif key == '8' then
        Selected = 8
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.7,0,0,0))
    elseif key == '9' then
        Selected = 9
        script.Parent.Hotbar.Selecter:TweenPosition(UDim2.new(0.8,0,0,0))
    end
end)

game:GetService('RunService').RenderStepped:connect(function()
    wait(.1)
    local Slot1V = string.sub(HotbarItems[1],1,2)
    local LegitSlot1 = string.upper(Slot1V)
    script.Parent.Hotbar.Slot1.Text = LegitSlot1
    local Slot2V = string.sub(HotbarItems[2],1,2)
    local LegitSlot2 = string.upper(Slot2V)
    script.Parent.Hotbar.Slot2.Text = LegitSlot2
    local Slot3V = string.sub(HotbarItems[3],1,2)
    local LegitSlot3 = string.upper(Slot3V)
    script.Parent.Hotbar.Slot3.Text = LegitSlot3
    local Slot4V = string.sub(HotbarItems[4],1,2)
    local LegitSlot4 = string.upper(Slot4V)
    script.Parent.Hotbar.Slot4.Text = LegitSlot4
    local Slot5V = string.sub(HotbarItems[5],1,2)
    local LegitSlot5 = string.upper(Slot5V)
    script.Parent.Hotbar.Slot5.Text = LegitSlot5
    local Slot6V = string.sub(HotbarItems[6],1,2)
    local LegitSlot6 = string.upper(Slot6V)
    script.Parent.Hotbar.Slot6.Text = LegitSlot6
    local Slot7V = string.sub(HotbarItems[7],1,2)
    local LegitSlot7 = string.upper(Slot7V)
    script.Parent.Hotbar.Slot7.Text = LegitSlot7
    local Slot8V = string.sub(HotbarItems[8],1,2)
    local LegitSlot8 = string.upper(Slot8V)
    script.Parent.Hotbar.Slot8.Text = LegitSlot8
    local Slot9V = string.sub(HotbarItems[9],1,2)
    local LegitSlot9 = string.upper(Slot9V)
    script.Parent.Hotbar.Slot9.Text = LegitSlot9
end)
--[[--[[--[
Inventories
--]]--]]--]
Row = 0
MaxSlotsPerRow = 9
Slot = 0
LastSlot = nil
script.Parent:WaitForChild('CreativeInventory')

for index, value in ipairs(ItemList) do
    Slot = Slot + 1
    local BlockName = string.sub(value,1,2)
    local BlockTag = string.upper(BlockName)
    BlockButton = game:GetService('Lighting').Item:Clone()
    BlockButton.Parent = script.Parent.CreativeInventory.Blocks
    if LastSlot == nil then
    BlockButton.Position = UDim2.new(BlockButton.Position.X.Scale + .1, 0, Row, 0)
    else
    BlockButton.Position = UDim2.new(LastSlot.Position.X.Scale + .1, 0, Row, 0)
    end
    BlockButton.Text = BlockTag
    BlockButton.ItemTag.Value = value
    CreativeItemClick(BlockButton)
    LastSlot = BlockButton
    if Slot >= MaxSlotsPerRow then
        Row = Row + 0.1
        Slot = 0
        BlockButton.Position = UDim2.new(0,0,Row,0)
    end
end
0
Oh hell no! TheUniPiggy 77 — 7y
0
XD DrPredablox 153 — 7y

2 answers

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
7 years ago

Ok, I'm not going to actually neaten it for you, but here's what you can do. First of all, place the code in chunks if possible. So if 2 functions are kinda working together, or are for the same thing, place them together without a space in between them. Second of all, make other scripts. I assume this script is for something minecraft based. Rather having it all in one script, space it out a bit if you can. Maybe, inventory handler in one, placing handler in another. It helps alot so theres no need to look through like 200 lines for a single few lines of code.

0
Well almost every piece of code there requires the one table ItemList Thegrimdeathzombie 40 — 7y
0
Copy and paste the table to every script lol. Use remote events/functions if you need to. H4X0MSYT 536 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Alright not trying to be a douche but a «neat» code would be one thats easily readable and clean, I suggest making it more clean, and you should ALWAYS comment on certain lines so it's easier to get through, even if you're not going to share it with anyone.

Answer this question